Python Forum

Full Version: Putting column name to dataframe, can't work.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I tried to put a column name for normal dataframe by putting df.columns = ['ColumnName'] It works. But, after I pd.concat(df,df1), I can't put a column name to it. Any idea

I also tried df.rename = ['ColumnName']
Do you want to change the name of an existing column (rename) or add a column? Adding a column is easy:
df['New Column Name'] = values
Where "New Column Name" is the name you want to use to ID the column and values is the list/array/whatever that you want to add.
I totally understand your frustration. When you use pd.concat(df, df1), it’s combining your DataFrames, and you might need to rename the columns afterward. Instead of df.rename = ['ColumnName'], try using df.columns = ['ColumnName1', 'ColumnName2', ...] after the concatenation to match the number of columns in your combined DataFrame. df_combined = pd.concat([df, df1], axis=1) df_combined.columns = ['ColumnName1', 'ColumnName2', ...] Just make sure the number of column names matches the total number of columns in the new DataFrame.