Python Forum
Putting column name to dataframe, can't work.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Putting column name to dataframe, can't work.
#1
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']
Reply
#2
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create new column in dataframe Scott 10 603 Yesterday, 10:18 PM
Last Post: Scott
  attempt to split values from within a dataframe column mbrown009 9 2,791 Jun-20-2024, 07:59 PM
Last Post: AdamHensley
  concat 3 columns of dataframe to one column flash77 2 1,066 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 1,284 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  New Dataframe Column Based on Several Conditions nb1214 1 1,934 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  Setting the x-axis to a specific column in a dataframe devansing 0 2,153 May-23-2021, 12:11 AM
Last Post: devansing
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 2,438 May-17-2021, 07:02 AM
Last Post: lorensa74
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 3,134 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 4,425 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  Pandas DataFrame and unmatched column sritsv19 0 3,158 Jul-07-2020, 12:52 PM
Last Post: sritsv19

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020