DataFrame-按组搜索唯一值-有没有办法使它更快?

时间:2019-03-15 16:01:30

标签: python loops dataframe group-by

任务:
在某些行的产品,天,pgroup,价格(逻辑键=产品,天)列的数据框中,pgroup为空。如果该产品的其他数据集包含某个值,则应将其用于空数据集。

当前,我正在遍历产品,为每个产品搜索组的唯一值。 我想更快地做到这一点。

示例:

数据:

df = pd.DataFrame([['a','2018-02-03','G1',47],
              ['a','2018-02-04',None,25],
              ['a','2018-02-05','G1',10],
              ['a','2018-02-06',None,22],
              ['a','2018-02-07',None,84],
              ['b','2018-02-03',None,10],
              ['b','2018-02-04',None,21],
              ['b','2018-02-05',None,2],
              ['b','2018-02-06','G2',18],
              ['b','2018-02-07','G2',11],
              ['c','2018-02-03','G2',63],
              ['c','2018-02-04','G2',83],
              ['c','2018-02-05',None,20],
              ['c','2018-02-06',None,68],
              ['c','2018-02-07',None,33]])
df.columns = ['product','day','pgroup', 'value']

代码:

# Loop for each product
for xprod in df['product'].unique().tolist():
    # find unique values for pgroup
    unique_values = df[df['product'] == xprod]['pgroup'].unique()
    # Change Datatypes because of NaN-Values in Series
    unique_values_str = [str(i) for i in unique_values]
    # 2 values, first is NaN => take second 
    if len(unique_values_str) == 2 and (unique_values_str[0] == 'nan'):
        df.loc[df['product'] == xprod, 'pgroup'] = unique_values_str[1]
    # 2 values, second is NaN => take first
    elif len(unique_values_str) == 2 and (unique_values_str[1] == 'nan'):
        df.loc[df['product'] == xprod, 'pgroup'] = unique_values_str[0] 

预期结果:

    product     day         pgroup  value
0   a           2018-02-03  G1      47
1   a           2018-02-04  G1      25
2   a           2018-02-05  G1      10
3   a           2018-02-06  G1      22
4   a           2018-02-07  G1      84
5   b           2018-02-03  G2      10
6   b           2018-02-04  G2      21
7   b           2018-02-05  G2      2
8   b           2018-02-06  G2      18
9   b           2018-02-07  G2      11
10  c           2018-02-03  G2      63
11  c           2018-02-04  G2      83
12  c           2018-02-05  G2      20
13  c           2018-02-06  G2      68
14  c           2018-02-07  G2      33

注释:
根据我的检查,大部分时间是前两行:

 # Loop for each product
    for xprod in df['product'].unique().tolist():
        # find unique values for pgroup
        unique_values = df[df['product'] == xprod]['pgroup'].unique()

1 个答案:

答案 0 :(得分:1)

这感觉有点棘手,我真的不知道它将如何执行,但是我认为应该更快一些。

df2 = df
df2['pgroup'] = df.groupby(['product'])['pgroup'].transform(lambda x : repr(set(x) - set([None]) ).replace("{'",'').replace("'}",'') )

如果prep会引起值问题,那么您可能还必须改变它对repr产生的字符串的处理方式。