I have two dataframes, one 18x30 (called df1) and one 2x30 (called df2), both of them have exactly the same index values.
I want to be able to add one of the columns from df2 to the end of df1.
The data types in df1 are all integer and the data type for df2 is string. Whenever I merge/concat/join, I get NaN instead of the right data.
Any help would be greatly appreciated
Thanks :D
Using apply() method If you need to apply a method over an existing column in order to compute some values that will eventually be added as a new column in the existing DataFrame, then pandas. DataFrame. apply() method should do the trick.
append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value.
Add multiple columns to a data frame using Dataframe. assign() method. Using DataFrame. assign() method, we can set column names as parameters and pass values as list to replace/create the columns.
It is possible to join the different columns is using concat() method. DataFrame: It is dataframe name. axis: 0 refers to the row axis and1 refers the column axis. join: Type of join.
if you want to add the column at the end, you can use
df1['columename']= df2['existing_colume_name']
and after that apply
df1.column_name = df1.column_name.astype(float)
This worked for me !
correct answer:
df1['column_name'] = df2['column_name'].values
the thing is you need to pass an object of a certain type for it to work correctly. List or array are preferable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With