简体   繁体   中英

Pandas - Convert dictionary to dataframe - keys as columns

I have a folder with .csv files that contain timeseries in the following format:

1   0.950861
2   2.34248
3   2.56038
4   3.46226
...

I access these textfiles by looping over the folder containing the files and passing each textfile to a dictionary:

data_dict = {textfile: pd.read_csv(textfile, header=3, delim_whitespace=True, index_col=0) for textfile in textfiles}

I want to merge the columns, which contain the data next to each other with the dictionary keys as index (Pathname of the textfiles). They all have the same row number.

So far I tried passing the dictionary to a pd.Dataframe like this:

df = pd.DataFrame.from_dict(data_dict, orient='index')

Actually, the orientation needs to be the default 'columns', but it results in an error: Value Error: If using all scalar values, you must pass an index

If I do so, I get the wrong result: Excel_Output

This is how I pass the frame to Excel:

writer = pd.ExcelWriter("output.xls")
df.to_excel(writer,'data', index_label = 'data', merge_cells =False)
writer.save()

I think the error must be in passing the dictionary to the dataframe. I tried pd.concat/merge/append but nothing returns the right result.

Thanks in Advance!

IIUC you can try list comprehension with concat :

data_list = [pd.read_csv(textfile, header=3, delim_whitespace=True, index_col=0) 
             for textfile in textfiles]
print (pd.concat(data_list, axis=1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM