根据多种条件分析数据框 [英] Analyzing a dataframe based on multiple conditions

查看:114
本文介绍了根据多种条件分析数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

names   Class   Category    label
ram     A        Red        one
ravi    A        Red        two
gopal   B        Green      three
Sri     C        Red        four      

my_list1=["Category"]
my_list2=["Class"]

I need to get the combination counts between these two columns. 

我正在尝试获取某些选定列的组合。 my_list2甚至有多个。

I am trying to get the combination of some selected columns. my_list2 even have more than one.

 I tried, 
 df[mylist1].value_counts()

对于sinigle列来说,它工作正常。但是我想根据my_list1在my_list2中做多列

It is working fine for a sinigle column. But I want to do for multiple column in my_list2 based on my_list1

我想要的输出应该是

output_df,
 Value     Counts
 Red.A      2
 Red.C      1
 Green.B    1


推荐答案

您可以使用 str.cat

In [5410]: my_list1 = ["Category"]
      ...: my_list2 = ["Class", "Class1"]

In [5411]: df[my_list1+my_list2].apply(lambda x: x.str.cat(sep='.'), axis=1).value_counts()
Out[5411]:
Green.B.B    1
Red.A.E      1
Red.A.G      1
dtype: int64

In [5516]: pd.Series('.'.join(x) for x in df[my_list1 + my_list2].values).value_counts()
Out[5516]:
Green.B.B    1
Red.A.E      1
Red.A.G      1
dtype: int64

Or

In [5517]: pd.Series(map('.'.join, df[my_list1 + my_list2].values)).value_counts()
Out[5517]:
Green.B.B    1
Red.A.E      1
Red.A.G      1
dtype: int64

这篇关于根据多种条件分析数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆