简体   繁体   English

用 python 重写 csv 文件

[英]rewrite csv file with python

I have csv file with this structure:我有 csv 这个结构的文件:

code1     code2     code3      name1     name2    sometnig1   something2

14355     12345     54133      part1     part12   aaaaaaaa    bbbbbbb
54782     57815     52781      part2     part22   ccccccc     ffffffff
14515     52495     52852      part3     part33   ddddddd     sssssss

I need to parse this csv file and create my new csv file with my own headers and only columns, that I need, for example:我需要解析这个 csv 文件并使用我自己的标题和我需要的列创建新的 csv 文件,例如:

code_1    code_2    name_1    name_2   something_2

14355     12345     part1     part12   bbbbbbb
54782     57815     part2     part22   ffffffff
14515     52495     part3     part33   sssssss

I know, that I can select one column that I need and write it to another file using pandas:我知道,我可以 select 我需要的一列,然后使用 pandas 将其写入另一个文件:

df = pd.read_csv(file)
df1 = df[code_1]

But how can I select multiple columns and write in one file?但是我怎样才能将 select 多列写入一个文件呢?

The easiest would be to read only the columns you care about, and save some memory too:最简单的方法是只阅读您关心的专栏,并保存一些 memory:

df = pd.read_csv(file, usecols=["code_1", "code_2", "name_1", "name_2", "something_2"])
df.to_csv("other_file.csv", index=False)

Another option, if you already have a df you want to subset, is to use a list to select the columns you care about.另一种选择,如果你已经有一个你想要子集的df ,是使用一个列表到 select 你关心的列。

df = df[["code_1", "code_2", "name_1", "name_2", "something_2"]]

You can select multiple columns by using a list:您可以使用列表 select 多列:

df1 = df[['code1', 'code2', 'name1', 'name2', 'something2']]

You can then change the column names using another list:然后您可以使用另一个列表更改列名:

df1.columns = ['code_1', 'code_2', 'name_1', 'name_2', 'something_2']

then you can write that back to a csv然后你可以把它写回 csv

df1.to_csv('new filname.csv')

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

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