简体   繁体   English

如何在条件变化的函数中匹配DataFrame列上的值?

[英]How to match values on DataFrame column in function of a changing condition?

I have one panda DataFrame match with NaN values in Col 3 and i'm trying to replace it with the correct value present only once in the Col 3 in function of the associated string in Col 2 : 我在第3列中有一个与NaN值匹配的熊猫DataFrame,我试图用第2列中相关字符串的函数在第3列中仅用一次存在的正确值替换它:

What I have: 我有的:

Col 1 | 第1栏 | Col 2 | 第2栏 | Col 3 第3列

1 | 1 | 'GE1' | 'GE1'| '1.2' '1.2'

2 | 2 | 'GE1' | 'GE1'| NaN N

3 | 3 | 'GE1' |NaN 'GE1'| NaN

4 |'GE2' |'1.3' 4 |'GE2'|'1.3'

5 |'GE2'|NaN 5 |'GE2'| NaN

I've tried this : (match_without_nan is the same DF than match but with only the rows without NaN) 我已经试过了:(match_without_nan是与match相同的DF,但只有没有NaN的行)

`` ` ```

mylist=[[match_without_nan['Col 2'],match_without_nan['Col 3']]]

for i in range(0,len(mylist)):
    match.loc[match['Col 2'] == mylist[0][i], 'Col 3'] = mylist[1][i]

` `` ```

This code is running without any errors but nothing changed in Col 3. 该代码正在运行,没有任何错误,但在Col 3中没有任何更改。

What I would like to get : 我想得到什么:

Col 1 | 第1栏 | Col 2 | 第2栏 | Col 3 第3列

1 | 1 | 'GE1' | 'GE1'| '1.2' '1.2'

2 | 2 | 'GE1' | 'GE1'| '1.2' '1.2'

3 | 3 | 'GE1' | 'GE1'| '1.2' '1.2'

4 |'GE2' |'1.3' 4 |'GE2'|'1.3'

5 |'GE2'|'1.3' 5 |'GE2'|'1.3'

I don't understand fully your code, and why it doesn't work, but i would just use "sql-like" operators to achieve your goal: 我不完全理解您的代码,以及为什么它不起作用,但是我只使用“类似于sql的”运算符来实现您的目标:

df = pd.DataFrame({
  "col2": ['GE1', 'GE1', 'GE1', 'GE2', 'GE2'],
  "col3": [np.nan, '1.2', np.nan, '1.3', np.nan]
})

unique_values = df.groupby("col2").first()
final_df = df.join(unique_values, on="col2", lsuffix="_orig")

The groupby will extract the mapping from your col2 keys to your unique values present in col3, and the join will generate the final table you want. groupby将提取col2键到col3中存在的唯一值的映射,然后联接将生成所需的最终表。

IIUC, you want to do ffill with groupby : IIUC,您想用groupby填充

df['Col 3'] = df.groupby('Col 2')['Col 3'].transform(lambda x: x.ffill())
print(df)

Output: 输出:

   Col 1  Col 2  Col 3
0      1  'GE1'  '1.2'
1      2  'GE1'  '1.2'
2      3  'GE1'  '1.2'
3      4  'GE2'  '1.3'
4      5  'GE2'  '1.3'

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

相关问题 如何在特定条件下将某个 function 应用于 DataFrame 列值 - How to apply a certain function to DataFrame column values on a certain condition 条件 function 对 dataframe 中列的值进行计数和求和 - Condition function on counting and summation of values of a column in a dataframe 如何在 dataframe 中按条件重新分配列中的值? - How to reassign values in column by condition in dataframe? 如何使用基于条件的值将 append 列到 dataframe - How to append a column to a dataframe with values based on condition 如何根据第二个 Dataframe 值的条件替换 Dataframe 列值 - How to Replace Dataframe Column Values Based on Condition of Second Dataframe Values 根据条件更改数据框中的值 - Changing values in a dataframe based on condition 如何根据数据框中的列条件应用函数 - How to apply function on the basis of column condition in a dataframe 如何在给定条件的情况下用 pandas dataframe 中的另一列替换一列的值 - How to replace the values of a column with another column in pandas dataframe given a condition 如何根据条件从数据框列名称填充列值? - How to fill column values from dataframe column names based on condition? 如何根据具有一系列值的条件替换 pd 数据框列中的值? - How to Replace values in a pd dataframe column based on a condition with a range of values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM