Jan-21-2025, 07:52 PM
(This post was last modified: Jan-21-2025, 07:52 PM by deanhystad.)
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.
Are you reading the data from a csv file? The whitespace around the "|" are a problem. If my data looks like this:
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.