Created
April 19, 2024 05:27
-
-
Save hdary85/ca7c2822a4c11e561e5aa5e0e50a2d1c to your computer and use it in GitHub Desktop.
Merge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
merged = pd.merge(df1, df2, on='C1', how='left') | |
# Grouping by 'C1' and checking for multiple occurrences | |
grouped = merged.groupby('C1') | |
# Creating the new DataFrame with custom values for multiple occurrences | |
df3 = pd.DataFrame(columns=merged.columns) | |
for name, group in grouped: | |
if len(group) > 1: | |
df3 = df3.append({col: 'multiple' if col == 'C2' else 'MULTIPLE' for col in merged.columns}, ignore_index=True) | |
else: | |
df3 = df3.append(group.iloc[0], ignore_index=True) | |
print(df3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment