Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dataframe merge
#2
It appears to be straight forward. The "trick" is only merging some columns from the second dataframe. This can be accomplished by merging a dataframe that only contains the columns you want to merge.

The code below uses a dataframe slice that only has the PZN and MB columns.
import pandas as pd
from io import StringIO

data1 = StringIO(
    """PZN|Name|Anzahl
1234|Aspirin|1
2345|Mexalen|2
4567|Sinupret|9
4567|Sinupret|2
"""
)
data2 = StringIO(
    """PZN|MB|Zeichen
1234|1|+
2345|2|++
4567|0|++
"""
)

df1 = pd.read_csv(data1, sep="|")
df2 = pd.read_csv(data2, sep="|")
print(df1)
print(df2)  # All columns
print(df2[["PZN", "MB"]])  # Only PZN and MB columns
df3 = df1.merge(df2[["PZN", "MB"]], on="PZN")
print(df3)
Output:
PZN Name Anzahl 0 1234 Aspirin 1 1 2345 Mexalen 2 2 4567 Sinupret 9 3 4567 Sinupret 2 PZN MB Zeichen 0 1234 1 + 1 2345 2 ++ 2 4567 0 ++ PZN MB 0 1234 1 1 2345 2 2 4567 0 PZN Name Anzahl MB 0 1234 Aspirin 1 1 1 2345 Mexalen 2 2 2 4567 Sinupret 9 0 3 4567 Sinupret 2 0
Is that the desired output?

Are you reading the data from a csv file? The whitespace around the "|" are a problem. If my data looks like this:
Output:
data1 = StringIO( """PZN | Name | Anzahl 1234|Aspirin|1 2345|Mexalen|2 4567|Sinupret|9 4567|Sinupret|2 """ ) data2 = StringIO( """PZN|MB|Zeichen 1234|1|+ 2345|2|++ 4567|0|++ """ )
I get an error on the merge because one table has "PZN " as a header and the other has "PZN". The best way to fix the problem is clean it up in the files.
Reply


Messages In This Thread
dataframe merge - by gunther - Jan-21-2025, 03:44 PM
RE: dataframe merge - by deanhystad - Jan-21-2025, 07:52 PM
RE: dataframe merge - by Pedroski55 - Jan-22-2025, 05:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How can I merge several images via loop using the information of a dataframe? noahverner1995 1 2,083 Dec-31-2021, 05:03 AM
Last Post: noahverner1995
  Mail Merge from DataFrame into Word Label Template PEGylated_User 0 2,635 Nov-10-2020, 01:01 PM
Last Post: PEGylated_User
  Pands DataFrame merge question FORTITUDE 0 2,351 Sep-20-2018, 01:14 PM
Last Post: FORTITUDE

Forum Jump:

User Panel Messages

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