[英]How to save python list(dictionary) to csv file
I have the following list(dictionary):我有以下列表(字典):
db = [{'Cat1': DB_Field1 DB_Field2 DB_Field3
4 1F1 1F2 1F3
5 2F1 2F2 2F3
6 3F1 3F2 3F3
8 NaN NaN NaN
11 NaN NaN NaN
14 NaN NaN NaN},
{'Cat2': DB_Field4 DB_Field5
4 1F4 1F5
5 2F4 2F5
6 3F4 3F5
8 NaN NaN
11 NaN NaN
14 NaN NaN},
{'Cat3': DB_Field6
4 1F6
5 2F6
6 3F6
8 NaN
11 NaN
14 NaN},
{'Cat4': DB_Field7 DB_Field7 DB_Field8 DB_Field9
4 1F7 1F7 1F8 1F9
5 2F7 2F7 2F8 2F9
6 3F6 3F6 3F8 3F9
8 NaN NaN NaN NaN
11 NaN NaN NaN NaN
14 NaN NaN NaN NaN},
{'Cat5': DB_Field10 DB_Field11 DB_Field12 DB_Field13 DB_Field13 DB_Field13 \
4 1F10 1F11 1F12 1 1 1
5 2F10 2F11 2F12 2 2 2
6 3F10 3F11 3F12 3 3 3
8 NaN NaN NaN NaN NaN NaN
11 NaN NaN NaN NaN NaN NaN
14 NaN NaN NaN NaN NaN NaN
DB_Field13 DB_Field13
4 1 1
5 2 2
6 3 3
8 NaN NaN
11 NaN NaN
14 NaN NaN }]
I would like to save it in the following format:我想将其保存为以下格式:
How do I do that?我怎么做?
What I tried so far?到目前为止我尝试了什么?
pd.DataFrame(db).to_csv('test.csv')
This does not save entire dictionary only the keys are saved like the following:这不会保存整个字典,仅保存键,如下所示:
One idea is use concat
with DataFrame.dropna
and DataFrame.to_csv
, but get repeated first values of data in csv, because is written MultiIndex
:一个想法是将
concat
与DataFrame.dropna
和DataFrame.to_csv
使用,但在 csv 中获得重复的第一个数据值,因为它是MultiIndex
:
pd.concat(db, axis=1).dropna().to_csv('test.csv', index=False)
EDIT: If there are nested DataFrames use first concat
for join them to list of DataFrames and then use second concat
for one DataFrame
:编辑:如果有嵌套的 DataFrames 使用第一个
concat
将它们加入 DataFrames 列表,然后将第二个concat
用于一个DataFrame
:
(pd.concat([pd.concat(x, axis=1) for x in db], axis=1)
.dropna().to_csv('test.csv', index=False))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.