Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add value in one dataframe to a new column in another dataframe

I have two DataFrames of different sizes.

df1 = pd.DataFrame()
df2 = pd.DataFrame()
df1["id"] = ["A", "A", "B", "B", "C", "C"]
df1["revenue"] = [1, 2, 10, 9, 11, 14]
df2["id"] = ["A", "B", "C"]
df2["revenue"] = [12, 1, 5]

enter image description here
enter image description here

I want to create a new column in df1 titled revenue_year_1 and fill it with the revenue values in df2 conditional on the id values being equal. So in df1 in both lines where the id is ‘A’ revenue_year_1 would be 12, where the id is ‘B’ the revenue_year_1 would be 1 and where the id is C the revenue_year_1 would be 5. In practice I’m doing this on a larger data set where I would not be able to do this by hand.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desired outcome:

enter image description here

>Solution :

You could also merge on "id":

df1 = df1.merge(df2, on='id', suffixes=('','_year_1'))

Output:

  id  revenue  revenue_year_1
0  A        1              12
1  A        2              12
2  B       10               1
3  B        9               1
4  C       11               5
5  C       14               5
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading