如何使用Pandas根据另一列值获取列的中值?

2024-04-23 08:24:01 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从这个数据帧中获取所有Gender = 'Woman'ConvertedComp列的中位数(它只显示“Man”,因为我只显示df.head(10)):

^{tb1}$

Tags: 数据dfgenderhead中位数mantb1woman
1条回答
网友
1楼 · 发布于 2024-04-23 08:24:01

按性别分组,并返回折算薪酬的中位数-

In [1]:
df.groupby('gender')['convertedcomp'].median()

Out [1]:
gender
man      910
woman     42
Name: convertedcomp, dtype: int64

#this worked in the notebook, not sure the difference in syntax between the two answers, someone might be able to shine some light on that
In [2]:
data = {'convertedcomp': [61, 951, 910, 30, 42, 899], 'gender': ['man', 'man', 'man', 'woman', 'woman', 'woman']}

df = pd.DataFrame.from_dict(data)

df.groupby('gender').median('convertedcomp')

Out [2]:
    convertedcomp
gender  
man     910
woman   42

相关问题 更多 >