Python Dataframe-B更新Dataframe-A

news/2024/10/21 6:03:49/

假设现在有两个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 pddef my_update(df_updater, df_updatee, based_column_name, update_column_name):# Create a mapping dictionary from the df_updater DataFramemapping_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 dictionarydf_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 datatypedf_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)


http://www.ppmy.cn/news/705992.html

相关文章

《基于AidLux的自动驾驶智能预警应用方案》

YOLOP模型转ONNX ONNX是开放式神经网络(Open Neural Network Exchange)的简称,ONNX的规范及代码主要由微软,亚马逊,Facebook和IBM等公司共同开发,以开放源代码的方式托管在Github上。目前官方支持加载ONNX模型的框架有&#xff1…

IOS如何安装ipa文件

https://www.i4.cn/pro_ios.html#jiaocheng 用电脑下载 爱思助手PC端 然后电脑连接 苹果手机, 用 安装的 爱思助手PC端 软件 安装 “爱思助手移动端” 下载并安装 “爱思助手PC端” ,打开爱思助手PC端 用数据线连接设备到电脑,连接成功后&am…

直接下载ipa包

itms-services://?actiondownload-manifest&urlxxx.plist

查看ipa内容

经过Xcode编译生成的ipa文件实际上就是一个zip文件。我们把ipa文件的后缀名改成“zip”,然后双击即可解压打开。解压后的程序在Payload目录下,是一个“app”后缀的文件夹。在文件上单击右键,选择“显示包内容(Show Package Conten…

iPhone 直接安装 .ipa包

有些App因为各种原因,不能直接通过App Store下载到,以前是可以在网上找到.ipa文件通过iTunes直接安装的。但是现在的版本已经不支持了。 官方的工具不好用,只好用民间的了。我们在电脑上下载一个爱思助手,然后将手机连接到电脑上…

IAP2

先来简单的 我们做另外一个程序 串口发送过去 然后之前API1的工程跳到新程序 第一步:制作新bin 参考之前的文章即可。 功能我们完全复制API的工程 修改一点点变化 做好new.bin。 while (1){HAL_Delay(1000);HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);printf(&…

苹果如何安装ipa

懒省事使用爱思助手即可 1.下载cydiaimpactor 官方地址 百度云下载:https://pan.baidu.com/s/1rYIG4go-fOEHarSjziA1eg 提取码:3b48 2.连上苹果手机,启动cydiaimpactor,导入安装包 3.输入苹果账号密码(密码为App 专用…

Google Play IAP 相关

-------------- 基本的流程注意事项 ------------- 1.发布的程序必须是alpha版及以上,不能是内部测试版。 内部测试版是不能测支付的 2.测试的时候,使用的账号不能是发布程序的账号。 并且要进行以下三项工作才能完成测试账号的设置: a 在谷…