pands 不熟悉,就是拿来用一下,发现这个奇怪现象
代码:
import pandas as pd
# 初始化一个空的 DataFrame
df = pd.DataFrame()
# 添加列数据
df['Name'] = ['Alice', 'Bob', 'Charlie']
df['Age'] = [25, 30, 35]
df_a = df.copy()
df_b = df.copy()
df_c = df.copy()
df_d = df.copy()
df_e = df.copy()
meger_df = df.merge(df_a, on='Name', how='left', suffixes=('_x', '_a'))
print(meger_df)
meger_df = meger_df.merge(df_b, on='Name', how='left', suffixes=('_a', '_b'))
print(meger_df)
meger_df = meger_df.merge(df_c, on='Name', how='left', suffixes=('_b', '_c'))
print(meger_df)
meger_df = meger_df.merge(df_d, on='Name', how='left', suffixes=('_c', '_d'))
print(meger_df)
meger_df = meger_df.merge(df_e, on='Name', how='left', suffixes=('_d', '_e'))
print(meger_df)
1
placeless 241 天前 1
列名冲突时,suffix/prefix 才有效
1. df < df_a => Age + Age => Age_x, Age_a 2. df_a < df_b => Age_x, Age_a, + Age => Age_x, Age_a, Age 3. df_b < df_c => Age_x, Age_a, Age, +Age => Age_x, Age_a, Age_b, Age_c 以此类推 |