Python Dataframe-B更新Dataframe-A

假设现在有两个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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Toblerone_Wind

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值