[英]Formatting a csv file by deleting and transforming columns with Pandas Python
I want to modify a data frame in python with the pandas function.我想用 pandas function 修改 python 中的数据框。 So I want to delete the first and the 7th column
(Unix Timestamp, Close)
.所以我想删除第一列和第七列
(Unix Timestamp, Close)
。 I have also moved the column Symbol
to the end of the columns.我还将列
Symbol
移动到列的末尾。 How could I do these transformations to the csv file below.我怎样才能对下面的 csv 文件进行这些转换。 I want the formats to be written in the csv file permanently, would this be possible?
我希望将格式永久写入 csv 文件中,这可能吗?
import pandas as pd
url= input.csv
data = pd.read_csv(url, low_memory=False)
original csv:原装 csv:
Firstly drop your columns by drop()
method:-首先通过
drop()
方法删除列:-
data=data.drop(columns=['Unix Timestamp','Close'])
Now use pop()
method:-现在使用
pop()
方法:-
symbol=data.pop('Symbol')
Finally:-最后:-
data['Symbol']=symbol
After that if you wants to change the contents of csv file then save this data to your csv file by to_csv()
method之后,如果您想更改 csv 文件的内容,然后通过
to_csv()
方法将此数据保存到您的 csv 文件中
data.to_csv(url)
Note:- If you don't want to save index then pass index=False
in to_csv()
method注意:-如果您不想保存索引,请在
to_csv()
方法中传递index=False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.