首页 > 编程语言 >python pandas DataFrame-A 更新 DataFrame-B中指定列相同的数据

python pandas DataFrame-A 更新 DataFrame-B中指定列相同的数据

时间:2024-05-26 10:55:01浏览次数:15  
标签:python text update column DataFrame label df pandas

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

# 使用 DataFrame B 中的“text”列作为索引、“label”列作为值创建映射字典
mapping_dict = B.set_index('text')['label'].to_dict()

# 使用 map()函数遍历A['text'],并将mapping_dict中对应key的value传给A['text']列然后用fillna(A['label'])中的值替换未匹配而出现的NaN值
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)

原贴:

https://blog.csdn.net/qq_42276781/article/details/131511441

标签:python,text,update,column,DataFrame,label,df,pandas
From: https://www.cnblogs.com/vPYer/p/18213417

相关文章