[英]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.