Home > Software design >  How to add a column to a pandas dataframe without column?
How to add a column to a pandas dataframe without column?

Time:11-18

I have a dataframe wich has 2 'columns'. The first column does not seem to have a column name, the second one is named Speed.

When I do print(df.columns) and print(df) I get the following:

Index(['Speed'], dtype='object')
            Speed
0  0.000   26.788
1  0.027   26.807
2  0.053   26.860

Now I want to give the first column the name Power, so I tried this:

df_level2.columns = ['Power']

This was my output when I did the same print statements again:

Index(['A'], dtype='object')
                A
0  0.000   26.788
1  0.027   26.807
2  0.053   26.860

This is my desired output:

Index(['Power', 'Speed'], dtype='object')
   Power   Speed
0  0.000   26.788
1  0.027   26.807
2  0.053   26.860

CodePudding user response:

Did you already try to set the whole column list?

df.columns = ["Power", "Speed"]

CodePudding user response:

Your column is probably called '' right now. You can look at df.columns to verify.

You can rename all columns by assigning to columns:

df.columns = ['Power', 'Speed']

You can also rename just one.

df = df.rename(columns={'': 'Power'})
  • Related