我正在尝试在csv文件的最后一行和特定列中插入文本,我已经在Internet上搜索了,但是找不到任何解决方案。
在下面的示例中,我只想添加一封电子邮件。
例子:
Name, Age, email
Jake, 23, [email protected]
Camila, , Camila,outlook.com
, , [email protected] <- The line that I want to add
我的代码:
df = pd.DataFrame(columns=header)
df.to_csv(str(day) + '_' + str(month) + '_' + str(year) + '.csv',
sep=',',
header=True,
index=False)
df.loc[df.shape[0], 'Campaign'] = 'hi'
您必须to_csv
用作最后一行:
header = ["Name", "Age", "email"]
df = pd.DataFrame(columns=header)
df.loc[df.shape[0], 'email'] = '[email protected]'
df.to_csv("stackoverflow.csv",
index=False)
输出:
Name,Age,email
,,[email protected]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句