计算 pandas 行中值的外观 [英] Count appearance of a value in a pandas row
问题描述
嘿,我目前正在熊猫中的一列中进行迭代.
现在,我想处理一个值在该列中只出现一次的情况,而不是在它出现多次时的情况.
我尝试了几种方法,但是都没有用.
现在我得到了错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我只是尝试首先将所有带有id
i
的行过滤到df_path_counter
中,然后我将其视为if子句中的行.我不知道为什么它不起作用.
有任何想法吗?
这是我的代码:
Hey I am currently iterating through a column in pandas.
Now i want to handle the case that a value appears only once in that column different than when it appears multiple times.
I tried several approaches but none worked.
Right now I get the error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I just try to filter first all rows with the id
i
into df_path_counter
and than i count it is rows in the if clause. I don't know why it does not work.
Any ideas?
This is my code:
df_path = pd.DataFrame([(1, 'Germany'),
(1, 'France'),
(1, 'Indonesia'),
(1, 'France'),
(2, 'France'),
(1, 'Germany'),
(1, 'UK'),
],
columns=['id', 'country']
for i, g in df_path.groupby('id'):
df_path_counter=df_path.loc[df_path['id'] == i]
if(df_path_counter.count()<=1):
#...do sth
推荐答案
根据建议,使用Series.value_counts
创建一个count == 1的国家/地区列表,并使用带有Series.isin
的布尔索引进行过滤:
As suggested, use Series.value_counts
to create a list of country with count == 1 and use boolean indexing with Series.isin
to filter:
country_counts = df_path['country'].value_counts()
country_1 = country_counts[country_counts.eq(1)].index
df_path[df_path['country'].isin(country_1)]
[出]
id country
2 1 Indonesia
6 1 UK
这篇关于计算 pandas 行中值的外观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!