简体   繁体   English

Append 星号到现有的 dataframe 值,基于与另一个 dataframe 中的 dataframe 的掩码

[英]Append asterisk to existing dataframe value, based on mask with another dataframe in Python

I have two dataframes:我有两个数据框:

import pandas as pd
data1 = {1: [1,2,3], 2: [1,2,3]}
df1 = pd.DataFrame(data1)

data2 = {1: [49,60,12], 2: [14,70,38]}
df2 = pd.DataFrame(data2)

Where a value is >= 30 but <50 in df2, I want to append an * to the respective existing value in df1.如果 df2 中的值 >= 30 但 <50,我想 append 和 df1 中相应的现有值。 I tried:我试过了:

df1 = df1.mask((df2 >= 30) & (df2 < 50), [df1 + '*'])

But this gives an error.但这给出了一个错误。

Thanks in advance.提前致谢。

You are close, remove [] and convert values to strings for add * :您已关闭,删除[]并将值转换为字符串以添加*

df = df1.mask((df2 >= 30) & (df2 < 50), df1.astype(str) + '*')
print (df)
    1   2
0  1*   1
1   2   2
2   3  3*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 pandas 中另一个 dataframe 的掩码值在 dataframe 中制作 NaN - Make NaN in a dataframe based on mask value of another dataframe in pandas Python Pandas:仅当列值唯一时,才将数据框追加到另一个数据框 - Python Pandas: Append Dataframe To Another Dataframe Only If Column Value is Unique 从python中的现有数据框中追加数据框中的行 - Append Rows in a dataframe from an existing dataframe in python 如何 append dataframe 到现有的 Excel 文件基于带有 Z23EEEB4347BDD755BFC6B7EE9DDZA 的标头 - How to append dataframe to existing Excel file based on headers with python 将数据框附加到另一个数据框的列和Python常量 - Append a dataframe with a column of another dataframe and a constant with Python 根据另一个数据帧的条件值,从现有数据帧中创建一个新数据帧 - create a new dataframe out of a existing dataframe, based on conditional value of another dataframe Python Pandas基于掩码获取数据框的一部分 - Python Pandas get part of dataframe based on a mask 将日期时间戳作为列附加到 Python 中的现有 DataFrame - Append Date Timestamp as a column to existing DataFrame in Python 挣扎到 append dataframe 到 Python 中现有的.xlsx 文件 - Struggling to append dataframe to existing .xlsx file in Python append dataframe 到现有的 dataframe 有条件 - append dataframe in to existing dataframe with condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM