根据另一个列pandas数据框提取列值 [英] extract column value based on another column pandas dataframe
本文介绍了根据另一个列pandas数据框提取列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有点想提取一个变量的值,而条件又是另一个变量.例如,以下数据框:
I am kind of getting stuck on extracting value of one variable conditioning on another variable. For example, the following dataframe:
A B
p1 1
p1 2
p3 3
p2 4
当B=3
时如何获得A
的值?每次提取A
的值时,我都会得到一个对象,而不是字符串.
How can I get the value of A
when B=3
? Every time when I extracted the value of A
, I got an object, not a string.
推荐答案
您可以使用 iloc
以获得第一个元素:
You could use loc
to get series which satisfying your condition and then iloc
to get first element:
In [2]: df
Out[2]:
A B
0 p1 1
1 p1 2
2 p3 3
3 p2 4
In [3]: df.loc[df['B'] == 3, 'A']
Out[3]:
2 p3
Name: A, dtype: object
In [4]: df.loc[df['B'] == 3, 'A'].iloc[0]
Out[4]: 'p3'
这篇关于根据另一个列pandas数据框提取列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文