Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This script will convert the original groceries list.csv file in a perfect table
- Date: 30.01.2020
- Fahim Kamal
- """
- import pandas as pd
- # Read the csv file
- file = open('groceries list.csv')
- raw_data = file.readlines()
- number_of_columns = 0
- rows = []
- for row in raw_data:
- # Separate each items from the string
- rows.append(row.split(','))
- # Remove '\n' from the last element of the list
- rows[-1][-1] = rows[-1][-1][0:len(rows[-1][-1])-1]
- # Find the max number of column's from all rows
- number_of_columns = max(number_of_columns, len(rows[-1]))
- # Create the dictionary for the dataframe
- dictionary = {}
- for _ in range(number_of_columns):
- dictionary[f'Column {_ + 1}'] = []
- for row in rows:
- if _ < len(row):
- dictionary[f'Column {_ + 1}'].append(row[_])
- else:
- dictionary[f'Column {_ + 1}'].append(None)
- df = pd.DataFrame.from_dict(dictionary)
- df.to_csv('Sample_groceries_table.csv', index=False)
- # print(dictionary)
- # print(rows)
- # print(number_of_columns)
- print(df)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement