Home > Software design >  how to add two datafames together
how to add two datafames together

Time:12-10

I'm trying to add two dataframes and can't figure out if I should use merge or concatenate. Any help would be greatly appreciated. Here is the first Dataframe: df1

|   |temp|
|:--|:---|
|4  |NaN |
|9  |NaN |
|29 |NaN |
|89 |NaN |
|179|NaN |
|364|NaN |

while the second is df2

|   |UST|
|:--|:---|
|4  |62 |
|9  |59 |
|29 |41 |
|89 |31 |
|179|-9 |
|364|NaN |

I would like merge the two for both to look like this.

|   |temp|UST|
|:--|:---|:--|
|4  |NaN |62 |
|9  |NaN |59 |
|29 |NaN |41 |
|89 |NaN |31 |
|179|NaN |-9 |
|364|NaN |NaN|

CodePudding user response:

If I am understanding correctly, you basically just need to add the UST column from one data frame into another data frame (or selectively add columns from both into a separate merged data frame.

I would recommend using pandas as it makes data frame manipulation much easier in my opinion.

import pandas as pd

df1 = pd.DataFrame(data)
df2 = pd.DataFrame(df2data)

tempcol = df1["temp"]
USTcol = df2["UST"]

combinedframe = pd.DataFrame()
combinedframe["temp"] = tempcol
combinedframe["UST"] = USTcol

The index column should automatically be generated. Alternatively, for the first two lines of code, you could load the data in from a csv file.

df1 = pd.read_csv("name of csv file here")
df2 = pd.read_csv("name of csv file here") 

You could also concatenate on an axis

result = pd.concat([df1, df2], axis=1)

CodePudding user response:

If that first column is the index, then you should just use merge:

df1.merge(df2)

I believe the default is inner so it'll only keep the values that are in both dataframes: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html

  • Related