This is probably easy, but I have the following data:
In data frame 1:
index dat1 0 9 1 5
In data frame 2:
index dat2 0 7 1 6
I want a data frame with the following form:
index dat1 dat2 0 9 7 1 5 6
I've tried using the append
method, but I get a cross join (i.e. cartesian product).
What's the right way to do this?
In pandas you can add/append a new column to the existing DataFrame using DataFrame. insert() method, this method updates the existing DataFrame with a new column. DataFrame. assign() is also used to insert a new column however, this method returns a new Dataframe after adding a new column.
Here are two commands which can be used: Use Dataframe join command to append the columns. Use Pandas concat command to append the columns. Both methods can be used to join multiple columns from different data frames and create one data frame.
Use concat() to Append a Column in Pandas We can use the concat function in Pandas to merge or concatenate multiple data frames into one with the help of a single argument that is passed as an array with all the data frames to be combined.
It seems in general you're just looking for a join:
> dat1 = pd.DataFrame({'dat1': [9,5]}) > dat2 = pd.DataFrame({'dat2': [7,6]}) > dat1.join(dat2) dat1 dat2 0 9 7 1 5 6
You can also use:
dat1 = pd.concat([dat1, dat2], axis=1)
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