gpt4 book ai didi

python - 如何对我的 DataFrame 中的值进行分组和计数?

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:41 24 4
gpt4 key购买 nike

我有这个数据框:

df = pd.DataFrame(columns=["App","Feature1", "Feature2","Feature3",
"Feature4","Feature5",
"Feature6","Feature7","Feature8"],
data=[["SHA",0,0,1,1,1,0,1,0],
["LHA",1,0,1,1,0,1,1,0],
["DRA",0,0,0,0,0,0,1,0],
["FRA",1,0,1,1,1,0,1,1],
["BRU",0,0,1,0,1,0,0,0],
["PAR",0,1,1,1,1,0,1,0],
["AER",0,0,1,1,0,1,1,0],
["SHE",0,0,0,1,0,0,1,0]])

更新:(抱歉,我错误地表述了预期结果)

我想计算每个特征值 1 出现的次数:

Features   Count
Feature1 6
Feature2 7
...

我试过这个:

df.groupBy("App").count()

但我没有得到预期的输出。

最佳答案

使用:

#remove column App, compare and get sum of Trues
a0 = df.drop('App', 1).eq(0).sum()
#a0 = df.set_index('App').eq(0).sum()

#alternative with select only Feature columns
#a0 = df.filter(like='Feature').eq(0).sum()

#alternative with select all columns without first
a0 = df.iloc[:, 1:].eq(0).sum()

print (a0)
Feature1 6
Feature2 7
Feature3 2
Feature4 2
Feature5 4
Feature6 6
Feature7 1
Feature8 7
dtype: int64

1 比较类似:

a1 = df.drop('App', 1).eq(1).sum()
#a1 = df.set_index('App').eq(1).sum()

#alternative
#a1 = df.filter(like='Feature').eq(1).sum()
#alternative
a1 = df.iloc[:, 1:].eq(1).sum()

print (a1)
Feature1 2
Feature2 1
Feature3 6
Feature4 6
Feature5 4
Feature6 2
Feature7 7
Feature8 1
dtype: int64

全部与value_counts一起:

a = df.drop('App', 1).apply(pd.value_counts).T.add_prefix('count_')
print (a)
count_0 count_1
Feature1 6 2
Feature2 7 1
Feature3 2 6
Feature4 2 6
Feature5 4 4
Feature6 6 2
Feature7 1 7
Feature8 7 1

或者列表理解:

L = [df[x].value_counts() for x in df.columns.difference(['App'])]
a = pd.concat(L, 1).T.add_prefix('count_')
print (a)
count_0 count_1
Feature1 6 2
Feature2 7 1
Feature3 2 6
Feature4 2 6
Feature5 4 4
Feature6 6 2
Feature7 1 7
Feature8 7 1

关于python - 如何对我的 DataFrame 中的值进行分组和计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47137187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com