Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas
- def rename_columns(csv_file): # fuction to rename bad columns
- csv_file = csv_file.rename(
- columns={'Price $': 'Price', 'FBA Fees $': 'FBA_Fees', 'Active Sellers #': 'Active_Sellers',
- 'Product Details': 'Product_Details', 'Review Count': 'Review_Count',
- 'Review velocity': 'Review_velocity', 'Buy Box': 'Buy_Box', 'Size Tier': 'Size_Tier',
- 'Creation Date': 'Creation_Date'})
- return csv_file
- def drop_all_duplicates(csv_file):
- csv_file = csv_file.drop_duplicates().reset_index(drop=True)
- return csv_file
- def rename_words(csv): # fuction to rename all NANS to 'UNKNOWN'
- list = sum(csv.isna().sum())
- if list != 0:
- for element in csv.columns:
- csv[element] = csv[element].fillna('UNKNOWN')
- return csv
- def remove_all_commas(csv_file): # fucnction to remove all commas from csv file
- array = ['Price', 'Sales', 'Revenue', 'Review_Count', 'Review_velocity', 'BSR', 'FBA_Fees', 'Active_Sellers',
- 'Images', 'Weight']
- data_frame = pandas.DataFrame(csv_file)
- for element in array:
- i = 0
- for el in data_frame[element]:
- answer = ''
- stack = []
- if type(el) == str:
- if el != 'UNKNOWN':
- if ',' in el:
- for new_el in el:
- if new_el.isdigit() or new_el == '.':
- stack.append(new_el)
- else:
- continue
- for key in stack:
- answer += key
- if element in ['Sales', 'Review_Count']:
- data_frame.loc[i, element] = int(answer)
- i += 1
- else:
- data_frame.loc[i, element] = float(answer)
- i += 1
- else:
- if element in ['Sales', 'Review_Count']:
- result = int(el)
- data_frame.loc[i, element] = result
- i += 1
- else:
- result = float(el)
- data_frame.loc[i, element] = result
- i += 1
- else:
- i += 1
- return data_frame
- def check_the_types(csv_file): # have already yet
- for element in csv_file.columns:
- for el in csv_file[element]:
- print(el, ' ', type(el))
- return f'All types from all csv_file : {csv_file}'
- def remove_commas_test(csv_file): # bad function for future test
- pass
- def convet_to_csv(data_frame): # Fucton to convert data_frame as csv
- csv_file = data_frame.to_csv('tested_file_for_amazon.csv')
- return csv_file
- def convert_revenue_to_int(csv_file):
- index = 0
- for element in list(csv_file['Revenue']):
- if csv_file.loc[index, 'Revenue'] != 'UNKNOWN':
- csv_file.loc[index, 'Revenue'] = int(csv_file.loc[index, 'Revenue'])
- index += 1
- return csv_file
- def remove_unknown_from_Revenue(csv_file):
- index = 0
- for element in list(csv_file['Revenue']):
- if element == 'UNKNOWN':
- csv_file.loc[index, 'Revenue'] = 0
- index += 1
- return csv_file
- def sort_revenue(data_frame): # test
- data_frame = data_frame.sort_values('Revenue', ascending=False)
- return data_frame
- def check_types_of_str_columns(csv_file): # another fuction to check types
- stack_of_columns = ['Price', 'Sales', 'Revenue', 'Review_Count', 'Review_velocity', 'BSR', 'FBA_Fees']
- for name_of_column in stack_of_columns:
- for element in csv_file[name_of_column]:
- if type(element) == str:
- continue
- else:
- print(f'The tipy of element is: {type(element)} and the element is : {element}')
- if type(element) == str:
- raise Exception
- def check_test(csv_file): # fuction to understand types
- for element in csv_file.columns:
- for el in csv_file[element]:
- print(el, ' ', type(el))
- return False
- def find_sum_column_sales(csv_file): # Find sum of sales column
- result = 0
- for element in list(csv_file['Sales']):
- if element != 'UNKNOWN':
- result += int(element)
- return result
- def find_sum_of_revenue_column(csv_file): # Find sum of Revenue
- result = 0
- for element in list(csv_file['Revenue']):
- if element != 'UNKNOWN':
- result += int(element)
- return result
- def find_sum_of_price(csv_file): # Find sum of Price column
- result = 0
- for element in list(csv_file['Price']):
- if element != 'UNKNOWN':
- result += int(element)
- return result
- def find_sum_of_review_count(csv_file): # Find sum of Review_Count column
- result = 0
- for element in list(csv_file['Review_Count']):
- result += int(element)
- return result
- def convert_unkown_to_digit_if_needed(csv_file): # Function for future math - to rename all String to zero
- stack_of_columns = ['Price', 'Sales', 'Revenue', 'Review_Count', 'Ratings', 'Review_velocity', 'BSR', 'FBA_Fees',
- 'Active_Sellers']
- for name in stack_of_columns:
- i = 0
- for element in csv_file[name]:
- if type(element) == str:
- csv_file[name][i] = 0
- i += 1
- return csv_file
- def work_with_columns(csv_file): # Find sum of Revenue Column - second - for the most reason
- counter = 0
- for element in csv_file['Revenue']:
- counter += element
- return counter
- def create_sample(): # create sample for future csv
- rows_for_csv = [
- ['Face_Mask', 0, 0, 0, 0, 0, 0, 0, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
- [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'GR1', 'GR2',
- 'GR3', 'REST', '0-99', '100-499', '500-999', '1000'],
- [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '(1-3) G1R', '(4-9) G2R', '(10-16) G3R', '(16-58) G4R',
- '(1-3) G1AR', '(4-9) G2AR', '(10-16) G3AR', '(16-58) G4AR', 0, 0, 0, 0, 0, 0, 0, 0],
- [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 0, 0, 0, 0, 0, 0, 0, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ',
- ' '],
- [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '(1-3) G1RW', '(4-9) G2RW', '(10-16) G3RW',
- '(16-58) G4RW', '(1-3) G1ARW', '(4-9) G2ARW', '(10-16) G3ARW', '(16-58) G4ARW', ' ', ' ', ' ', ' ', 0,
- 0, 0, 0],
- [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 0, 0, 0, 0, 0, 0, 0, 0, ' ', ' ', ' ', ' ', ' ', ' ', ' ',
- ' ']
- ]
- columns_for_csv = ['Product', 'Main_Search_Terms', 'Revenue', 'Avg. Reviews', 'Review per Revenue (RPR)',
- 'Sales (GR1, GR2, GR3)', 'Review Velocity (GR1, GR2, GR3)', 'Review CVR (GR1, GR2, GR3)',
- 'Products per Page', 'GxR / GxRW', '1', '2', '3', 'GxAR / GxARW', '4', '5',
- '6',
- 'Groups Share', '7', '8', '9',
- '\tAvg. Revenue for Groups by Reviews and Listings Count',
- '10', '11', '12']
- data_frame = pandas.DataFrame(data=rows_for_csv, columns=columns_for_csv)
- return data_frame
- def check_types(csv_file): # have allready done
- for element in csv_file.columns:
- for el in csv_file[element]:
- print(el, ' : ', type(el))
- pass
- def find_revenue_sum(data_frame_sample, csv_file_as_data_frame):
- stack = [element for element in list(csv_file_as_data_frame['Revenue']) if element != 'UNKNOWN']
- result_sum = sum(stack)
- data_frame_sample.loc[0, 'Revenue'] = int(result_sum)
- return data_frame_sample
- def find_sum_of_revenue_columns(csv_file, sample_of_data_frame):
- summa = int(sum([x for x in list(csv_file['Revenue']) if x != 'UNKNOWN']))
- sample_of_data_frame.loc[0, 'Revenue'] = summa
- pass
- def find_mean_avg_reviews(csv_file, sample_of_data_frame): # ?
- summa = int(sum([x for x in list(csv_file['Review_Count']) if x != 'UNKNOWN']))
- length = len([x for x in list(csv_file['Review_Count']) if x != 'UNKNOWN'])
- mean = summa / length
- sample_of_data_frame.loc[0, 'Avg. Reviews'] = int(mean)
- return sample_of_data_frame
- def review_per_revenue(sample_data_frame):
- sample_data_frame.loc[0, 'Review per Revenue (RPR)'] = int(
- sample_data_frame.loc[0, 'Revenue'] / sample_data_frame.loc[
- 0, 'Avg. Reviews'])
- return sample_data_frame
- def find_sum_of_sales(csv_file, sample_of_data_frame): # proverit' na real case
- stack = csv_file['Sales'][0:16]
- filtered_array = [x for x in stack if x != 'UNKNOWN']
- sample_of_data_frame.loc[0, 'Sales (GR1, GR2, GR3)'] = sum(filtered_array)
- return sample_of_data_frame
- def find_sum_review_velocity(sample_data_frame, csv_file):
- array_of_velocity = csv_file['Review_velocity'][0:16]
- array_of_velocity = [x for x in array_of_velocity if x != 'UNKNOWN']
- sample_data_frame.loc[0, 'Review Velocity (GR1, GR2, GR3)'] = sum(array_of_velocity)
- return sample_data_frame
- def find_percent_velocity_sales(sample_data_frame):
- result = (sample_data_frame.loc[0, 'Review Velocity (GR1, GR2, GR3)'] / sample_data_frame.loc[
- 0, 'Sales (GR1, GR2, GR3)']) * 100
- result = str(result)
- stack = []
- counter = 0
- for element in result:
- stack.append(element)
- counter += 1
- if counter == 4:
- break
- answer = ''
- for element in stack:
- answer += element
- sample_data_frame.loc[0, 'Review CVR (GR1, GR2, GR3)'] = answer + '%'
- return sample_data_frame
- def product_pet_page(csv_file, sample_data_frame):
- lenght = len(csv_file)
- sample_data_frame.loc[0, 'Products per Page'] = lenght
- return sample_data_frame
- def find_avg_review(csv_file, data_frame):
- stack_of_revenue = csv_file['Revenue'][0:3]
- filtered_array = [x for x in stack_of_revenue if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- length = len(filtered_array)
- data_frame.loc[3, 'GxAR / GxARW'] = str(int(result_sum / length)) + '$'
- return data_frame
- def find_sum_group_1_3(csv_file, sample_data_frame):
- stack_of_revenue = csv_file['Revenue'][0:3]
- filtered_array = [x for x in stack_of_revenue if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- sample_data_frame.loc[3, 'GxR / GxRW'] = str(int(result_sum)) + '$'
- return sample_data_frame
- def csv_file_to_data_frame_and_back(csv_file):
- data_frame = pandas.DataFrame(csv_file)
- realod_csv = data_frame.to_csv('for_boys_answers.csv')
- return realod_csv
- def find_avg_second_review(csv_file, data_frame):
- stack_of_group = csv_file['Revenue'][3:9]
- filtered_array = [x for x in stack_of_group if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- length = len(filtered_array)
- data_frame.loc[3, '4'] = str(int(result_sum / length)) + '$'
- return data_frame
- def find_sum_group_4_9(csv_file, sample_data_frame):
- stack_of_group = csv_file['Revenue'][3:9]
- filtered_array = [x for x in stack_of_group if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- sample_data_frame.loc[3, '1'] = str(int(result_sum)) + '$'
- return sample_data_frame
- def find_third_revenue_avg(csv_file, data_frame):
- stack_group = csv_file['Revenue'][9:16]
- filtered_array = [x for x in stack_group if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- length = len(filtered_array)
- data_frame.loc[3, '5'] = str(int(result_sum / length)) + '$'
- return data_frame
- def find_sum_revenue_10_16(csv_file, sample_data_frame):
- stack_group = csv_file['Revenue'][9:16]
- filtered_array = [x for x in stack_group if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- sample_data_frame.loc[3, '2'] = str(int(result_sum)) + '$'
- return sample_data_frame
- def find_fourth_avg_reviews(csv_file, data_frame):
- stack_group = csv_file['Revenue'][16:len(csv_file)]
- filtered_array = [x for x in stack_group if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- length = len(filtered_array)
- data_frame.loc[3, '6'] = str(int(result_sum / length)) + '$'
- return data_frame
- def find_sum_17_to_last(csv_file, sample_data_frame):
- stack_group = csv_file['Revenue'][16:len(csv_file)]
- filtered_array = [x for x in stack_group if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- sample_data_frame.loc[3, '3'] = str(int(result_sum)) + '$'
- return sample_data_frame
- def find_avg_review_count_1(csv_file, data_frame):
- stack_review_count = csv_file['Review_Count'][0:3]
- filtered_stack = [x for x in stack_review_count if x != 'UNKNOWN']
- result_sum = sum(filtered_stack)
- length = len(filtered_stack)
- data_frame.loc[5, 'GxAR / GxARW'] = str(int(result_sum / length))
- return data_frame
- def find_sum_review_count_1_3(csv_file, sample_data_frame):
- stack_review_count = csv_file['Review_Count'][0:3]
- filtered_stack = [x for x in stack_review_count if x != 'UNKNOWN']
- result_sum = sum(filtered_stack)
- sample_data_frame.loc[5, 'GxR / GxRW'] = int(result_sum)
- return sample_data_frame
- def find_avg_review_count_2(csv_file, data_frame):
- stack = csv_file['Review_Count'][3:9]
- filtered_stack = [x for x in stack if x != 'UNKNOWN']
- sum_result = sum(filtered_stack)
- length = len(filtered_stack)
- data_frame.loc[5, '4'] = str(int(sum_result / length))
- return data_frame
- def find_sum_review_count_4_9(csv_file, sample_data_frame):
- stack = csv_file['Review_Count'][3:9]
- filtered_stack = [x for x in stack if x != 'UNKNOWN']
- sum_result = sum(filtered_stack)
- sample_data_frame.loc[5, '1'] = int(sum_result)
- return sample_data_frame
- def find_avg_review_count_3(csv_file, data_frame):
- filtered_stack = [x for x in list(csv_file['Review_Count'][9:16]) if x != 'UNKNOWN']
- sum_result = sum(filtered_stack)
- length = len(filtered_stack)
- data_frame.loc[5, '5'] = str(int(sum_result / length))
- return data_frame
- def find_sum_review_count_10_16(csv_file, sample_data_frame):
- filtered_stack = [x for x in list(csv_file['Review_Count'][9:16]) if x != 'UNKNOWN']
- sum_result = sum(filtered_stack)
- sample_data_frame.loc[5, '2'] = int(sum_result)
- return sample_data_frame
- def find_avg_review_count_4(csv_file, data_frame):
- stack_review_count = csv_file['Review_Count'][16:len(csv_file)]
- filtered_array = [x for x in stack_review_count if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- length = len(filtered_array)
- data_frame.loc[5, '6'] = str(int(result_sum / length))
- return data_frame
- def find_sum_review_count_16_last(csv_file, sample_data_frame):
- stack_review_count = csv_file['Review_Count'][16:len(csv_file)]
- filtered_array = [x for x in stack_review_count if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- sample_data_frame.loc[5, '3'] = int(result_sum)
- return sample_data_frame
- def remove_dollar_from_revenue_groups(data_frame):
- stack_of_replacement = ['GxR / GxRW', '1', '2', '3']
- for element in stack_of_replacement:
- data_frame.loc[3, element] = data_frame.loc[3, element].replace('$', '')
- return data_frame
- def replace_type_revenue_groups(data_frame):
- stack_of_replacement = ['GxR / GxRW', '1', '2', '3']
- for element in stack_of_replacement:
- data_frame.loc[3, element] = int(data_frame.loc[3, element])
- return data_frame
- def all_types_review_count_to_int(csv_file):
- stack = ['Review_Count']
- counter = 0
- for element in stack:
- for el in csv_file[element]:
- csv_file.loc[counter, element] = int(csv_file.loc[counter, element])
- return csv_file
- def sort_for_review_count(csv_file):
- csv_file = csv_file.sort_values('Review_Count', ascending = False)
- return csv_file
- def find_sum_from_zero_to_hundred(csv_file, data_frame):
- new_csv = csv_file[csv_file['Review_Count'] < 100]
- filtered_array = [x for x in list(new_csv['Revenue']) if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- data_frame.loc[2, 'Avg. Revenue for Groups by Reviews and Listings Count'] = int(result_sum)
- return data_frame
- def find_sum_from_hundred_to_5_hundred(csv_file, data_frame):
- new_csv = csv_file[csv_file['Review_Count'] > 100]
- new_csv = new_csv[new_csv['Review_Count'] < 500]
- filtered_array = [x for x in list(new_csv['Revenue']) if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- data_frame.loc[2, '10'] = int(result_sum)
- return data_frame
- def find_sum_from_5_hundred_to_1_thousand(csv_file, data_frame):
- new_csv = csv_file[csv_file['Review_Count'] < 1000]
- new_csv = new_csv[new_csv['Review_Count'] > 500]
- filtered_array = [x for x in list(new_csv['Revenue']) if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- data_frame.loc[2, '11'] = int(result_sum)
- return data_frame
- def find_sum_more_than_thousand(csv_filem, data_frame):
- new_csv = csv_filem[csv_filem['Review_Count'] > 1000]
- filtered_array = [x for x in list(new_csv['Revenue']) if x != 'UNKNOWN']
- result_sum = sum(filtered_array)
- data_frame.loc[2, '12'] = int(result_sum)
- return data_frame
- def groups_share_1(data_frame):
- #formula : GxR / sum(revenue) * 100
- first_group = data_frame.loc[3, 'GxR / GxRW']
- result_sum = data_frame.loc[0, 'Revenue']
- data_frame.loc[2, 'Groups Share'] = str(int((first_group / result_sum) * 100)) + '%'
- return data_frame
- def groups_share_2(data_frame):
- second_group = data_frame.loc[3, '1']
- result_sum = data_frame.loc[0, 'Revenue']
- data_frame.loc[2, '7'] = str(int((second_group / result_sum) * 100)) + '%'
- return data_frame
- def groups_share_3(data_frame):
- third_group = data_frame.loc[3, '2']
- result_sum = data_frame.loc[0, 'Revenue']
- data_frame.loc[2, '8'] = str(int((third_group / result_sum) * 100 )) + '%'
- return data_frame
- def groups_share_delta(data_frame):
- delta_group = data_frame.loc[3, '6']
- result_sum = data_frame.loc[0, 'Revenue']
- data_frame.loc[2, '9'] = str(int((delta_group / result_sum) * 100)) + '%'
- return data_frame
- def length_reviews_0_99(csv_file, data_frame):
- new_csv = csv_file[csv_file['Review_Count'] < 100]
- length = len(new_csv)
- data_frame.loc[4, '\tAvg. Revenue for Groups by Reviews and Listings Count'] = length
- return data_frame
- def lenth_reviews_100_500(csv_file, data_frame):
- new_csv = csv_file[csv_file['Review_Count'] > 100]
- new_csv = new_csv[new_csv['Review_Count'] < 500]
- length = len(new_csv)
- data_frame.loc[4, '10'] = length
- return data_frame
- def length_reviews_500_1000(csv_file, data_frame):
- new_csv = csv_file[csv_file['Review_Count'] > 500]
- new_csv = new_csv[new_csv['Review_Count'] < 1000]
- data_frame.loc[4, '11'] = len(new_csv)
- return data_frame
- def length_reviews_more_then_thousand(csv_file, data_frame):
- new_csv = csv_file[csv_file['Review_Count'] > 1000]
- length = len(new_csv)
- data_frame.loc[4, '12'] = length
- return data_frame
- def swap_int_to_str_groups(data_frame):
- stack_of_groups = ['GxR / GxRW', '1', '2', '3']
- for element in stack_of_groups:
- data_frame.loc[3, element] = str(data_frame.loc[3, element]) + '%'
- return data_frame
- def remove_all_digits_from_columns(data_frame):
- data_frame = data_frame.rename(columns = {'1' : ' ', '2' : ' ', '3' : ' ', '4' : ' ', '5' : ' ', '6' : ' ', '7' : ' ', '8' : ' ', '9' : ' ', '10' : ' ', '11' : ' ', '12' : ' '})
- return data_frame
- def swap_int_to_str(data_frame):
- data_frame.loc[0, 'Revenue'] = str(data_frame.loc[0, 'Revenue']) + '$'
- return data_frame
- if __name__ == '__main__':
- file_for_amazon_csv = pandas.read_csv('"D:\naruto\Helium_10_Xray_2022-02-12.csv"')
- file_for_amazon_csv = pandas.DataFrame(file_for_amazon_csv) # test
- file_for_amazon_csv = rename_columns(file_for_amazon_csv)
- file_for_amazon_csv = drop_all_duplicates(file_for_amazon_csv)
- file_for_amazon_csv = rename_words(file_for_amazon_csv)
- file_for_amazon_csv = remove_all_commas(file_for_amazon_csv)
- file_for_amazon_csv = convert_revenue_to_int(file_for_amazon_csv)
- file_for_amazon_csv = remove_unknown_from_Revenue(file_for_amazon_csv)
- file_for_amazon_csv = sort_revenue(file_for_amazon_csv)
- data_frame = create_sample()
- data_frame = find_revenue_sum(data_frame, file_for_amazon_csv)
- data_frame = find_mean_avg_reviews(file_for_amazon_csv, data_frame)
- data_frame = review_per_revenue(data_frame)
- data_frame = find_sum_of_sales(file_for_amazon_csv, data_frame)
- data_frame = find_sum_review_velocity(data_frame, file_for_amazon_csv)
- data_frame = find_percent_velocity_sales(data_frame)
- data_frame = product_pet_page(file_for_amazon_csv, data_frame)
- data_frame = find_sum_group_1_3(file_for_amazon_csv, data_frame)
- data_frame = find_sum_group_4_9(file_for_amazon_csv, data_frame)
- data_frame = find_sum_revenue_10_16(file_for_amazon_csv, data_frame)
- data_frame = find_sum_17_to_last(file_for_amazon_csv, data_frame)
- data_frame = find_sum_review_count_1_3(file_for_amazon_csv, data_frame)
- data_frame = find_sum_review_count_4_9(file_for_amazon_csv, data_frame)
- data_frame = find_sum_review_count_10_16(file_for_amazon_csv, data_frame)
- data_frame = find_sum_review_count_16_last(file_for_amazon_csv, data_frame)
- data_frame = find_avg_review(file_for_amazon_csv, data_frame)
- data_frame = find_avg_second_review(file_for_amazon_csv, data_frame)
- data_frame = find_third_revenue_avg(file_for_amazon_csv, data_frame)
- data_frame = find_fourth_avg_reviews(file_for_amazon_csv, data_frame)
- data_frame = find_avg_review_count_1(file_for_amazon_csv, data_frame)
- data_frame = find_avg_review_count_2(file_for_amazon_csv, data_frame)
- data_frame = find_avg_review_count_3(file_for_amazon_csv, data_frame)
- data_frame = find_avg_review_count_4(file_for_amazon_csv, data_frame)
- file_for_amazon_csv = convet_to_csv(data_frame)
Advertisement
Add Comment
Please, Sign In to add comment