Python将csv导入dict
对不起。我是个笨蛋 我看到 如果我有这个Python将csv导入dict,python,csv,dictionary,Python,Csv,Dictionary,对不起。我是个笨蛋 我看到 如果我有这个 country population population_time EUR Germany 82521653.0 2016-12-01 True France 66991000.0 2017-01-01 True Indonesia 255461700.0 2017-01-01 False Ireland 4761865.0
country population population_time EUR
Germany 82521653.0 2016-12-01 True
France 66991000.0 2017-01-01 True
Indonesia 255461700.0 2017-01-01 False
Ireland 4761865.0 NaT True
Spain 46549045.0 2017-06-01 True
Vatican NaN NaT True
如何像这样改变
[{'country': 'Germany', 'population': 82521653.0, 'population_time': Timestamp('2016-12-0100:00:00'), 'EUR': True},
{'country': 'France', 'population': 66991000.0, 'population_time': Timestamp('2017-01-01 00:00:00'), 'EUR': True},
{'country': 'Indonesia', 'population': 255461700.0, 'population_time': Timestamp('2017-01-01 00:00:00'), 'EUR': False},
{'country': 'Ireland', 'population': 4761865.0, 'population_time': NaT, 'EUR': True},
{'country': 'Spain', 'population': 46549045.0, 'population_time': Timestamp('2017-06-01 00:00:00'), 'EUR': True},
{'country': 'Vatican', 'population': nan, 'population_time': NaT, 'EUR': True}]
import pandas as pd
df = pd.read_csv('file.csv')
print(df.to_dict())
我只能这样做
[{'country': 'Germany', 'population': 82521653.0, 'population_time': Timestamp('2016-12-0100:00:00'), 'EUR': True},
{'country': 'France', 'population': 66991000.0, 'population_time': Timestamp('2017-01-01 00:00:00'), 'EUR': True},
{'country': 'Indonesia', 'population': 255461700.0, 'population_time': Timestamp('2017-01-01 00:00:00'), 'EUR': False},
{'country': 'Ireland', 'population': 4761865.0, 'population_time': NaT, 'EUR': True},
{'country': 'Spain', 'population': 46549045.0, 'population_time': Timestamp('2017-06-01 00:00:00'), 'EUR': True},
{'country': 'Vatican', 'population': nan, 'population_time': NaT, 'EUR': True}]
import pandas as pd
df = pd.read_csv('file.csv')
print(df.to_dict())
结果:
{'country':{0:'Germany', 1:'France', 2:'Indonesia', 3:'Ireland', 4:'Spain', 5:'Vatican'},
'population ':{0:'82521653.0', 1:'66991000.0', 2:'255461700.0', 3:'4761865.0', 4:'46549045.0', 5:'NaN'},
'population_time':{0:'2016-12-01', 1:'2017-01-01', 2:'2017-01-01', 3:'NaT', 4:'2017-06-01', 5:'NaT'},
'EUR':{0:'True', 1:'True', 2:'False', 3:'True', 4:'True', 5:'True'},
尝试:
df.to_dict(orient='records')
您可以使用csv.DictReader
进行以下操作:
导入csv
打开('t.csv')作为f:
reader=csv.DictReader(f,分隔符='\t')
数据=[d代表读取器中的d]
差不多了。对你的代码稍作修改。注意关键字参数“orient”
将熊猫作为pd导入
df=pd.read\u csv('file.csv'))
打印(df.to_dict(orient='records'))
它给出以下输出
[{'country':'德国','人口':82521653.0,'人口时间':'2016-12-01','欧元':True},{'country':'法国','人口':66991000.0,'人口时间':'2017-01-01','欧元':True},{'country':'印度尼西亚','人口':255461700.0,'人口时间':'2017-01-01','欧元':False},{'country':'Ireland','population_time':'NaT','EUR':True},{'country':'Spain','population':46549045.0,'population_time':'2017-06-01','EUR':True},{'country':'Vatican','population_time':nan,'population_time':'NaT EUR','True}]
将open('path/to/csv')作为infle:answer=list(csv.DictReader(infle))