【问题标题】:Adding percent column to data frame将百分比列添加到数据框中
【发布时间】:2017-09-02 03:26:53
【问题描述】:

我有一个如下所示的 pandas df:

User    Purchase_Count    Location_Count
1       2                 3
2       10                5
3       5                 1
4       20                4
5       2                 3
6       2                 3
7       10                5

我将如何添加一个列来计算坐标对(Purchse_Count[i], Location_Count[i]) 在总条目中的百分比。 例如,我希望 df 看起来像:

User    Purchase_Count    Location_Count    %
1       2                 3                 42.85
2       10                5                 28.57
3       5                 1                 14.28
4       20                4                 14.28
5       2                 3                 42.85
6       2                 3                 42.85
7       10                5                 28.57

【问题讨论】:

  • 我建议接受@juanpa.arrivillaga 的回答。它更快,可以说更惯用。

标签: python pandas data-science


【解决方案1】:

pandas 解决方案是使用groupby 然后transform:

In [43]: df
Out[43]:
   User  Purchase_Count  Location_Count
0     1               2               3
1     2              10               5
2     3               5               1
3     4              20               4
4     5               2               3
5     6               2               3
6     7              10               5

In [44]: total = len(df)

In [45]: df['percentage'] = df.groupby(['Purchase_Count', 'Location_Count']).transform(lambda r: r.count()/total)

In [46]: df
Out[46]:
   User  Purchase_Count  Location_Count  percentage
0     1               2               3    0.428571
1     2              10               5    0.285714
2     3               5               1    0.142857
3     4              20               4    0.142857
4     5               2               3    0.428571
5     6               2               3    0.428571
6     7              10               5    0.285714

编辑以提高可读性

In [53]: df['percentage'] = (df.groupby(['Purchase_Count', 'Location_Count'])
    ...:                     .transform(lambda r: r.count()/total))

In [54]: df
Out[54]:
   User  Purchase_Count  Location_Count  percentage
0     1               2               3    0.428571
1     2              10               5    0.285714
2     3               5               1    0.142857
3     4              20               4    0.142857
4     5               2               3    0.428571
5     6               2               3    0.428571
6     7              10               5    0.285714

编辑:

根据@piRSquared 的建议,您可以使用:

df.groupby(['Purchase_Count', 'Location_Count']).transform('count') / total

相反,初步测试表明它明显更快。

【讨论】:

  • 由于total 是不变的,你应该能够摆脱df.groupby(['Purchase_Count', 'Location_Count']).transform('count') / total 无论哪种方式+1
【解决方案2】:

将groupby 与size 和join 一起使用

cols = ['Purchase_Count', 'Location_Count']
df.join(df.groupby(cols).size().div(len(df)).rename('%'), on=cols)

   User  Purchase_Count  Location_Count         %
0     1               2               3  0.428571
1     2              10               5  0.285714
2     3               5               1  0.142857
3     4              20               4  0.142857
4     5               2               3  0.428571
5     6               2               3  0.428571
6     7              10               5  0.285714

旧答案

在元组上使用pd.value_counts

tups = df[['Purchase_Count', 'Location_Count']].apply(tuple, 1)
df.assign(**{'%': tups.map(pd.value_counts(tups, normalize=True))})

   User  Purchase_Count  Location_Count         %
0     1               2               3  0.428571
1     2              10               5  0.285714
2     3               5               1  0.142857
3     4              20               4  0.142857
4     5               2               3  0.428571
5     6               2               3  0.428571
6     7              10               5  0.285714

时机

【讨论】:

    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多