Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Python file for operations in groceries items
- Date: 29.01.2020
- """
- import pandas as pd
- # Read the data file
- # raw_data = open('groceries list.csv')
- data_rows = raw_data.readlines()
- # Separate each item and save them in a list
- data_matrix = [data.split(',') for data in data_rows]
- # Find all unique items and calculate how many time they appear in the file
- groceries_items = {}
- for data in data_matrix:
- # remove the '\n' from the last item in the list
- data[-1] = data[-1][0:len(data[-1]) - 1]
- for item in data:
- if item in groceries_items:
- # If item present in the dictionary then increment it's value by one
- groceries_items[item] += 1
- else:
- # Else create a new key in the dictionary
- groceries_items[item] = 1
- # Print the dictionary
- # print(groceries_items)
- for key, value in groceries_items.items():
- print(f'key: {key},\nvalues:{value}')
- print(f'Total items: {len(groceries_items)}')
- # Create the matrix for dataframe
- # matrix = [list(groceries_items.keys()), list(groceries_items.values())]
- matrix = {'Items': list(groceries_items.keys()), 'Count': list(groceries_items.values())}
- print(matrix)
- # Create the dataframe
- df = pd.DataFrame.from_dict(matrix)
- # Sort the dataframe
- df = df.sort_values(['Count'], ascending=False)
- # Create the csv file
- # df.to_csv('sample_groceries_output.csv', index=False)
- df.to_csv('final_groceries_output.csv', index=False)
- print(df)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement