[英]Indexing from unique values from a different column
I have a dataframe with a bunch of columns and rows, and I want to get the data in one column based on the unique values in another column. 我有一个包含一列列和行的数据框,并且我想根据另一列中的唯一值来获取一列中的数据。
flag name
0 1 bob
1 2 larry
2 1 alice
3 1 mary
4 3 peter
5 4 rick
if a use 如果使用
df['flag'].unique()
I get 1 2 3 4 我得到1 2 3 4
How do I get the names that correspond to those unique values? 如何获得与这些唯一值相对应的名称?
ie 即
flag name
0 1 bob
1 2 larry
4 3 peter
5 4 rick
It doesn't matter if I get bob, alice, or mary. 我得到鲍勃,爱丽丝还是玛丽都没关系。 I just need a name for that flag value.
我只需要该标志值的名称即可。
By using drop_duplicates
通过使用
drop_duplicates
df.drop_duplicates(['flag'])
Out[1036]:
flag name
0 1 bob
1 2 larry
4 3 peter
5 4 rick
Wen's answer is simpler, but another way is to use groupby()
and then take the first entry per group using nth()
: Wen的答案比较简单,但是另一种方法是使用
groupby()
,然后使用nth()
每个组的第一个条目:
import pandas as pd
df = pd.DataFrame({'flag':[1, 2, 1, 1, 3, 4],
'name':['bob', 'larry', 'alice', 'mary', 'peter', 'rick']})
print df.groupby('flag').nth(0)
Result: 结果:
name
flag
1 bob
2 larry
3 peter
4 rick
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.