假设现在有两个dataframe,分别是A和B,它们有相同的列text和label。现在想使用B的label来更新A的label,基于它们共同的text。
数据示例
import pandas as pd
# Sample DataFrames A and B
data_A = {'text': ['text1', 'text2', 'text3', 'text4'], 'label': [1, 0, 0, 1]}
data_B = {'text': ['text3', 'text1'], 'label': [1, 0]}
A = pd.DataFrame(data_A)
B = pd.DataFrame(data_B)
print(A)
print(B)
预期输出
方法1
# Create a mapping dictionary using the 'text' column as the key and the 'label' column as the value from DataFrame B
mapping_dict = B.set_index('text')['label'].to_dict()
# Use the `map()` function to update the 'label' column in DataFrame A
A['label'] = A['text'].map(mapping_dict).fillna(A['label'])
A['label'] = A['label'].astype(int)
print(A)
方法2
# Merge DataFrames on 'text' column, keeping only the 'label' column from df_B
merged_df = df_B[['text', 'label']].merge(df_A[['text']], on='text', how='right')
# Set the index of both DataFrames to 'text' for the update operation
df_A.set_index('text', inplace=True)
merged_df.set_index('text', inplace=True)
# Update the 'label' column in df_A with the values from the merged_df
df_A.update(merged_df)
# Reset the index of df_A
df_A.reset_index(inplace=True)
print(df_A)
将方法1改为函数形式
import pandas as pd
def my_update(df_updater, df_updatee, based_column_name, update_column_name):
# Create a mapping dictionary from the df_updater DataFrame
mapping_dict = df_updater.set_index(based_column_name)[update_column_name].to_dict()
update_column_type = df_updatee[update_column_name].dtype
# Update the specified column in the df_updatee DataFrame using the mapping dictionary
df_updatee[update_column_name] = df_updatee[based_column_name].map(mapping_dict).fillna(df_updatee[update_column_name])
# Convert the column datatype back to its original datatype
df_updatee[update_column_name] = df_updatee[update_column_name].astype(update_column_type)
# Example usage
data_A = {'text': ['text1', 'text2', 'text3', 'text4'], 'label': [1, 0, 0, 1], 'other': ['a', 'b', 'c', 'd']}
data_B = {'text': ['text3', 'text1'], 'label': [1, 0]}
A = pd.DataFrame(data_A)
B = pd.DataFrame(data_B)
my_update(B, A, 'text', 'label')
print(A)