在列中计算唯一值数量 - Pandas Python

2024-03-29 12:35:35 发布

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

这是我的数据集:

Unique_ID   No_of_Filings   Req_1   Req_2   Req_3   Req_4
 RCONF045   3               Blue    Red     White   Violet
 RCONF046   3               Blue    Red     White   Brown
 RCONF047   3               Blue    Red     White   Brown
 RCONF048   3               Black   Yellow  Green   N/A
 RCONF051   4               Black   Yellow  Green   N/A
 RCONF052   4               Black   Brown   Green   Orange

我从最后4列(Req_1到Req_4)提取了唯一的值,方法如下:

^{pr2}$

这是我需要的输出。频率=它在最后四列中显示的次数(例如黄色只显示两次)和备案数量=总和(如果要求在该行中,则无申请数)。例如,蓝色在前三行,所以是3+3+3=9,棕色在第二、第三和第六行,所以是3+3+4=10

Requirements    Frequency   Number of Filings
   Blue            3              9
   Black           3              11
   Red             3              9
   Brown           3              10
   White           3              9
   Green           3              11
   Yellow          2              7
   N/A             2              7
   Violet          1              3
   Orange          1              4

如何使用pandas在上面新创建的数据框中创建这两个列?在

谢谢


Tags: of数据idgreenblueredreqblack
3条回答

请注意,您可以使用np.unique立即获得uniques的计数(这已经完成了您的一个目标)。在

df= df.fillna('NA')
cols = ["Req_1","Req_2","Req_3","Req_4"]

u = pd.unique(df[cols].values.ravel("K"))
s = np.unique(df[cols].values.ravel("K"), return_counts=True)

df2 = pd.DataFrame({'colors': u}).fillna('N/A')
df2['freq'] = df2.colors.map(dict(zip(*s)))
df2['n'] = [df[(df[cols] == v).sum(1) >=1].No_of_Filings.sum() for v in df2.colors]

    colors  freq    n
0   Blue    3   9
1   Black   3   11
2   Red     3   9
3   Yellow  2   7
4   Brown   3   10
5   White   3   9
6   Green   3   11
7   Violet  1   3
8   N/A     2   0
9   Orange  1   4

您可以使用agg沿着这些思路做一些事情,但这需要事先进行一些重新塑造。这里有一种方法:

agg_df = (df.fillna('N/A').set_index(['Unique_ID', 'No_of_Filings'])
          .stack()
          .reset_index('No_of_Filings')
          .groupby(0)
          .agg(['sum', 'size'])
          .reset_index())

agg_df.columns = ['Requirements', 'Number of Filings', 'Frequency']

>>> agg_df
  Requirements  Number of Filings  Frequency
0        Black                 11          3
1         Blue                  9          3
2        Brown                 10          3
3        Green                 11          3
4          N/A                  7          2
5       Orange                  4          1
6          Red                  9          3
7       Violet                  3          1
8        White                  9          3
9       Yellow                  7          2
from collections import defaultdict

d = defaultdict(int)
for i, f, *r in df.values:
    for v in r:
        d[(v, 'filings')] += f
        d[(v, 'frequency')] += 1

pd.Series(d).unstack().rename_axis('reqs').reset_index()

     reqs  filings  frequency
0     NaN        7          2
1   Black       11          3
2    Blue        9          3
3   Brown       10          3
4   Green       11          3
5  Orange        4          1
6     Red        9          3
7  Violet        3          1
8   White        9          3
9  Yellow        7          2

相关问题 更多 >