0

I have a dataframe with empty column name df.info():

 #   Column           Non-Null Count  Dtype 
---  ------           --------------  ----- 
 0   Timestamp        11586 non-null  object
 1   Email address    11586 non-null  object
 2                    11586 non-null  object
 3   Score            11586 non-null  object
 4   ID               11586 non-null  object
 5   variable         11586 non-null  object
 6   Answer           11586 non-null  object

It can also be at different column index such as :

 #   Column           Non-Null Count  Dtype 
---  ------           --------------  ----- 
 0   Timestamp        11586 non-null  object
 1   Email address    11586 non-null  object
 2   Score            11586 non-null  object
 3                    11586 non-null  object
 4   ID               11586 non-null  object
 5   variable         11586 non-null  object
 6   Answer           11586 non-null  object

How can I drop the empty column name regardless the position?

user6308605
  • 577
  • 4
  • 15

2 Answers2

2

You can use this:

df = df[[x for x in df.columns if len(x)>=1]]

This approach does not care in which index the empty name is, it just takes every column that has a length of at least 1 or more.

Jonas Palačionis
  • 3,922
  • 4
  • 16
  • 35
0

assuming that you want to drop column 3

columns_list = [ 3 ] # index numbers of columns you want to delete
df = df.drop(columns=df.columns[columns_list])
Paul Brennan
  • 2,399
  • 3
  • 14
  • 20