Reputation: 55
Hello my question is how to save my dictionary in a csv file , I have managed to collect data from diffrent excel files and save it in a dictionary here's the code :
import os
from os import walk
import pandas as pd
path = r'C:\Users\Sarah\Desktop\IOMTest'
my_files = []
for (dirpath, dirnames, filenames) in walk(path):
my_files.extend([os.path.join(dirpath, fname) for fname in filenames])
print(my_files)
all_dicts_list = []
for file_name in my_files:
#Display sheets names using pandas
pd.set_option('display.width',300)
mosul_file = file_name
xl = pd.ExcelFile(mosul_file)
mosul_df = xl.parse(0, header=[1], index_col=[0,1,2])
#Read Excel and Select columns
mosul_file = pd.read_excel(file_name, sheet_name = 0 ,
index_clo=None, na_values= ['NA'], usecols = "A, E, G, H , L , M" )
#Remove NaN values
data_mosul_df = mosul_file.apply (pd.to_numeric, errors='coerce')
data_mosul_df = mosul_file.dropna()
#Save to Dictionary
datamosulx = data_mosul_df.to_dict()
all_dicts_list.append(datamosulx)
print(datamosulx)
My question is how to save
datamosulx = data_mosul_df.to_dict()
all_dicts_list.append(datamosulx)
As a csv file ? Thank you
Upvotes: 1
Views: 304
Reputation: 906
If you have a list of dict.
import csv
records = [{'first_name': 'Arun', 'age': 27, 'location': ['KL', 'IN']},
{'last_name': 'N A', 'first_name': 'Samuel', 'age': 30}] # Input. Here we have two rows and lets take the keys as column name.
heading_set = {k for row in records for k in list(row)} # Doing this since the keys are different in the given dict
print(f"heading is: {heading_set}")
with open('example1.csv', 'w') as opcsv:
writer = csv.DictWriter(opcsv, fieldnames=list(heading_set))
writer.writeheader()
for row in records:
for key in list(row):
if type(row.get(key)) == list:
row[key] = ",".join(row[key])
writer.writerow(row)
Upvotes: 0
Reputation: 2447
if somedict
is your dictionary, you can use something like this:
with open('mycsvfile.csv','wb') as f:
w = csv.writer(f)
w.writerows(somedict.items())
Upvotes: 1
Reputation: 421
on dataframe object call to_csv('new_csv_filename.csv')
function.
Try following code:
final_dataframe = pd.DataFrame(all_dicts_list)
final_dataframe.to_csv('new_csv_filename.csv')
Upvotes: 0
Reputation: 1112
You can use to below two formats to convert pandas to dictionary.
Also It is one of the most widely used modules in python.
Type 1: Dictionary with list as values.
import pandas as pd
struct = {'ID' : ['1','2','3'], 'COST' : ['10','20','30']}
pd.DataFrame(struct).to_csv('Test.csv',header=True,index=False)
Type 2: List containing dictionaries with matching keys.
import pandas as pd
struct = [{'ID': '1', 'COST': '10'}, {'ID': '2', 'COST': '20'},{'ID': '3', 'COST': '30'}]
pd.DataFrame(struct).to_csv('Test.csv',header=True,index=False)
We use these structure in order to provide a structure to our data.
Learning References Link : https://pypi.org/project/pandas/
Upvotes: 0