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

时间:2023-01-21 22:18:10

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行:

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"]

2 个解决方案

#1


0  

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.

我不推荐它,并且您应该确保没有真正的列从“无名”开始,但除此之外,这应该对您有用。

#2


0  

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)]

df。列= [x,如果不是x.startswith('无名')else i,x in enumerate(df列)]

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]

df。列= [x,如果不是x.startswith('无名')则为df列中的x]

#1


0  

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.

我不推荐它,并且您应该确保没有真正的列从“无名”开始,但除此之外,这应该对您有用。

#2


0  

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)]

df。列= [x,如果不是x.startswith('无名')else i,x in enumerate(df列)]

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]

df。列= [x,如果不是x.startswith('无名')则为df列中的x]