gpt4 book ai didi

python - 从 csv 文件读取时, Pandas 添加列

转载 作者:行者123 更新时间:2023-11-28 21:07:31 25 4
gpt4 key购买 nike

我想使用 pandas read_csv 读取 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

最佳答案

我想你需要参数 names in 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.

关于python - 从 csv 文件读取时, Pandas 添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41312251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com