简体   繁体   English

通过使用 Pandas Python 删除和转换列来格式化 csv 文件

[英]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:

在此处输入图像描述

Formatted csv/Expected Output:格式化 csv/预期 Output: 在此处输入图像描述

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.

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