[英]Appending a csv with dictionary values using pandas python
我的python脚本产生一个字典,如下所示:
================================================== ==============
TL&DR
通过字典创建数据from_dict
,我使用from_dict
方法使问题复杂化了。 感谢@Sword。
换句话说,仅当您要创建一个数据pd.DataFrame.from_dict
且所有键都在一个列中,所有值都在另一列中时,才需要pd.DataFrame.from_dict
。 在所有其他情况下,它与已接受答案中提到的方法一样简单。
================================================== ============
{u'19:00': 2, u'12:00': 1, u'06:00': 2, u'00:00': 0, u'23:00': 2, u'05:00': 2, u'11:00': 4, u'14:00': 2, u'04:00': 0, u'09:00': 7, u'03:00': 1, u'18:00': 6, u'01:00': 0, u'21:00': 5, u'15:00': 8, u'22:00': 1, u'08:00': 5, u'16:00': 8, u'02:00': 0, u'13:00': 8, u'20:00': 5, u'07:00': 11, u'17:00': 12, u'10:00': 8}
并且还会产生一个变量,例如full_name
(作为脚本的参数),其值为“ John”。
每次运行脚本时,它都会以上述格式为我提供字典和名称。
我想将其写入csv文件中,以便以后以以下格式进行分析:
FULLNAME | 00:00 | 01:00 | 02:00 | .....| 22:00 | 23:00 |
John | 0 | 0 | 0 | .....| 1 | 2 |
我产生的代码如下:
import collections
import pandas as pd
# ........................
# Other part of code, which produces the dictionary by name "data_dict"
# ........................
#Sorting the dictionary (And adding it to a ordereddict) in order to skip matching dictionary keys with column headers
data_dict_sorted = collections.OrderedDict(sorted(data_dict.items()))
# For the first time to produce column headers, I used .items() and rest of the following lines follows it.
# df = pd.DataFrame.from_dict(data_dict_sorted.items())
#For the second time onwards, I just need to append the values, I am using .values()
df = pd.DataFrame.from_dict(data_dict_sorted.values())
df2 = df.T # transposing because from_dict creates all keys in one column, and corresponding values in the next column.
df2.columns = df2.iloc[0]
df3 = df2[1:]
df3["FULLNAME"] = args.name #This is how we add a value, isn't it?
df3.to_csv('test.csv', mode = 'a', sep=str('\t'), encoding='utf-8', index=False)
我的代码产生以下csv
00:00 | 01:00 | 02:00 | …….. | 22:00 | 23:00 | FULLNAME
0 | 0 | 0 | …….. | 1 | 2 | John
0 | 0 | 0 | …….. | 1 | 2 | FULLNAME
0 | 0 | 0 | …….. | 1 | 2 | FULLNAME
我的问题有两个:
这个怎么样?
df = pd.DataFrame(data_dict, index=[0])
df['FullName'] = 'John'
编辑:
很难理解您执行操作的方式,但是看起来问题出在df.columns = df.iloc[0]
。 我上面提到的代码不需要分配列名或进行转置操作。 如果要在每次迭代中添加字典,请尝试:
data_dict['FullName'] = 'John'
df = df.append(pd.DataFrame(data_dict, index =[0]), ignore_index = True).reset_index()
如果每一行的名称可能不同,则df['FullName'] = 'John'
将使整列等于John。 因此,作为更好的步骤,在您的字典中创建一个名为“ FullName”的键,并使用适当的名称作为其值,以避免为整个列分配统一的值,即
data_dict['FullName'] = 'John'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.