从 csv 文件读取时,pandas 添加列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41312251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas add columns when read from a csv file
提问by running man
I want to read from a CSV file using pandas read_csv
. The CSV file doesn't have column names. When I use pandas to read the CSV file, the first row is set as columns by default. But when I use df.columns = ['ID', 'CODE']
, the first row is gone. I want to add, not replace.
我想使用 pandas 从 CSV 文件中读取read_csv
。CSV 文件没有列名。当我使用 Pandas 读取 CSV 文件时,第一行默认设置为列。但是当我使用时df.columns = ['ID', 'CODE']
,第一行不见了。我想添加,而不是替换。
df = pd.read_csv(CSV)
df
a 55000G707270
0 b 5l0000D35270
1 c 5l0000D63630
2 d 5l0000G45630
3 e 5l000G191200
4 f 55000G703240
df.columns=['ID','CODE']
df
ID CODE
0 b 5l0000D35270
1 c 5l0000D63630
2 d 5l0000G45630
3 e 5l000G191200
4 f 55000G703240
回答by jezrael
I think you need parameter names
in read_csv
:
我认为你需要参数names
在read_csv
:
df = pd.read_csv(CSV, names=['ID','CODE'])
names: array-like, default None
List of column names to use. If file contains no header row, then you should explicitly pass header=None. Duplicates in this list are not allowed unless mangle_dupe_cols=True, which is the default.
名称:类似数组,默认无
要使用的列名称列表。如果文件不包含标题行,那么您应该明确传递 header=None。除非 mangle_dupe_cols=True,这是默认值,否则不允许在此列表中重复。
回答by ZdaR
You may pass the column names at the time of reading the csv file itself as :
您可以在读取 csv 文件本身时将列名传递为:
df = pd.read_csv(csv_path, names = ["ID", "CODE"])
回答by Carles Mitjans
Use names
argument in function call to add the columns yourself:
names
在函数调用中使用参数自己添加列:
df = pd.read_csv(CSV, names=['ID','CODE'])
回答by MaxU
you need both: header=None
and names=['ID','CODE']
, because there are no column names/labels/headers in your CSV file:
您同时需要:header=None
和names=['ID','CODE']
,因为您的 CSV 文件中没有列名/标签/标题:
df = pd.read_csv(CSV, header=None, names=['ID','CODE'])