简体   繁体   English

在 Pandas Python 中添加一行并将值附加到列

[英]Adding a row and appending values to columns in Pandas Python

I am trying to append 10, 5, 890, 50, Finish for the Date,High,Low,Volume,Symbol at the end of the input.csv file.我正在尝试 append 10、5、890、50 10, 5, 890, 50, Finishinput.csv文件末尾为Date,High,Low,Volume,Symbol完成。 It will format the contents of the csv file permanently so I use data.to_csv(url,index=False) .它将永久格式化 csv 文件的内容,因此我使用data.to_csv(url,index=False) How would I be able to do that?我怎么能做到这一点?

import pandas as pd
url= 'input.csv'
data = pd.read_csv(url, low_memory=False)
data.to_csv(url,index=False)

Current output:当前 output:

Date,High,Low,Volume,Symbol
2021-01-15 00:04:00,39358.98,39273.24,0.4786072902,BTCUSD
2021-01-15 00:03:00,39362.37,39166.19,0.2911817448,BTCUSD
2021-01-15 00:02:00,39187.06,39017.72,6.2488076695,BTCUSD

Expected output:预期 output:

Date,High,Low,Volume,Symbol
2021-01-15 00:04:00,39358.98,39273.24,0.4786072902,BTCUSD
2021-01-15 00:03:00,39362.37,39166.19,0.2911817448,BTCUSD
2021-01-15 00:02:00,39187.06,39017.72,6.2488076695,BTCUSD
10 , 5, 890, 50, Finish 

Try with loc :尝试使用loc

data = pd.read_csv(url, low_memory=False)

data.loc[len(data)] = [10 , 5, 890, 50, 'Finish' ]
data.to_csv(url,index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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