简体   繁体   English

比较不同大小的数据框,如果满足条件则创建一个新的

[英]Compare Dataframes of different size and create a new one if condition is met

I need help to figure out the following problem:我需要帮助来解决以下问题:

I have two(2) dataframes of different sizes.我有两 (2) 个不同大小的数据框。 I need to compare the values and, if the condition is met, replace the values in Dataframe 1.我需要比较这些值,如果满足条件,请替换 Dataframe 1 中的值。

If the values for a Material and Char in Dataframe 1 are = "Y", I need to get the "Required or Optional" value from Dataframe 2. If it's Required, then I replace the "Y" with "Y_REQD" otherwise if it's Optional then replace "Y" with "Y_OPT".如果 Dataframe 1 中的 Material 和 Char 的值为 =“Y”,我需要从 Dataframe 2 中获取“必需或可选”值。如果是必需的,那么我将“Y”替换为“Y_REQD”,否则如果是可选然后将“Y”替换为“Y_OPT”。

I've been using For loops but now the code is getting too complicated which hints me this may not be the best way.我一直在使用 For 循环,但现在代码变得太复杂了,这暗示我这可能不是最好的方法。

Thanks in advance.提前致谢。

在此处输入图像描述

This is more like a pivot problem, then we can reindex the dataframe then sum这更像是一个pivot问题,然后我们可以reindex dataframe 然后求和

df1=df1.replace({'Y':'Y_'})+df2.pivot(*df2.columns).reindex_like(df1).fillna('')

Mostly agree with @WeNYoBen's answer.大多同意@WeNYoBen 的回答。 But to make it completely right, dataframe2 need to be revised using df.replace.但要使其完全正确,需要使用 df.replace 修改 dataframe2。

Short version:精简版:

df1=df1.replace({'Y':'Y_'})+df2.replace({'Rqd': 'REQD', 'Opt': 'OPT'}).pivot(*df2.columns).reindex_like(df1).fillna('')

Long version:长版:

# break short into steps 
# 1. replace
df2 = df2.replace({'Rqd': 'REQD', 'Opt': 'OPT'})

# 2. pivot
df2 = df2.pivot(*df2.columns)

# 3. reindex
df2 = df2.reindex_like(df1)

# 4. fillna(cleanup df with string form)
df2 = df2.fillna('')

# 5. map on df1 and add up with df2
df1=df1.replace({'Y':'Y_'})+df2

Hope it helps.希望能帮助到你。

暂无
暂无

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

相关问题 比较两个不同大小的数据帧并在 Pandas 中创建一个新列 - Compare two dataframes with different size and create a new column in Pandas 如何比较两个相同大小的数据框并创建一个新的数据框,而在列中没有具有相同值的行 - How to compare two dataframes of the same size and create a new one without the rows that have the same value in a column 如何比较两列不同的数据框并创建一个新的 - How to compare two columns of diffrent dataframes and create a new one 在python中比较两个具有不同形状和条件的数据框 - Compare two dataframes with different shapes and with condition in python Function 迭代两个不同的数据帧并在满足条件时填充一列 - Function to iterate two different dataframes and fill a column if condition is met 比较两个不同大小的列表列表并创建一个新列表 - Compare Two different size list of lists and create a new list 我想分组,然后创建一个新列,如果满足条件,它会从不同的列中获取值 - I want to groupby, and then, create a new column which takes a value from a different column if a condition is met 比较两个数据框的列并创建一个新的数据框 - Compare columns of two dataframes and create a new dataframe 有没有一种方法可以比较一列的值,然后根据是否满足条件来更新列表? - Is there a way to compare values of one column and then update a list based on if a condition is met? 比较 Pandas 中两个大小不等的 Dataframes 中的列以进行条件检查 - Compare columns in Pandas between two unequal size Dataframes for condition check
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM