简体   繁体   English

熊猫:读取csv时不要重命名列

[英]Pandas: do not rename columns when reading csv

Is it possible to avoid automatic naming of columns with empty names (resulting for instance in “Unnamed: 13”) when reading data with pandas.read_csv? 使用pandas.read_csv读取数据时,是否可以避免自动命名具有空名称的列(例如,导致“未命名:13”)?

  • Example

Name row in csv file: CSV文件中的名称行:

name_1;name_2;;name_4

Names generated by read_csv: read_csv生成的名称:

["name_1", "name_2", "Unnamed: 3", "name_4"]

Desired names: 所需名称:

["name_1", "name_2", "", "name_4"]

You can rename columns after loading the CSV: 您可以在加载CSV后重命名列:

def rename(col):
    if col.startswith("Unnamed: "):
        return ""
    else:
        return col

data.columns = [rename(col) for col in data.columns]

I would not recommend it, and you should make sure there are no actual columns starting with "Unnamed: ", but otherwise this should work for you. 我不建议这样做,您应该确保没有以“ Unnamed:”开头的实际列,但是否则这应该对您有用。

After you read it, you could do something like 阅读后,您可以执行类似的操作

df.columns = [x if not x.startswith('Unnamed') else i for i,x in enumerate(df.columns)]

That will replace the unnamed columns with an integer corresponding to that columns place. 这样会将未命名的列替换为与该列位置相对应的整数。 If you really want it to just be blank, you could do 如果您真的希望它只是空白,则可以

df.columns = [x if not x.startswith('Unnamed') else "" for x in df.columns]

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

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