如何将列分类为分类变量

时间:2018-01-30 07:17:50

标签: python pandas aggregate-functions pandas-groupby

Df.set_index("date")
Df["contact"].resample("m")
Df.groupby(df["date", "I'd_client"])["contact"].Count() 
Df.groupby(df["date", "I'd_client"]). Agg({"contact" :" count"}) 

我希望有2个DataFrame,这是第一年只有一行,每个联系人的总和在不同的列中分开,每月最后一行:

1)

df2=Df.groupby(df.index)
Df2=[["contact"]]
df2.apply(lambda x : x.count())

2)

class MyView(View):
    def get(self, request):
        results = Rental.objects.filter(**request.GET)
        return serializers.serialize('json', results)

我试过

success

或者我尝试创建另一个数据框

@JoinColumn

但它没有给我我想要的东西!!

1 个答案:

答案 0 :(得分:0)

我认为您需要groupby + size + unstack

df['date l'] = pd.to_datetime(df['date l'])

df1 = df.groupby(['ID_client l','contact']).size().unstack(fill_value=0)
print (df1)
contact      email  mail  sms  tel
ID_client l                       
1                2     1    1    0
2                0     0    0    2

df11 = (df.groupby(['ID_client l', df['date l'].dt.year,'contact'])
         .size()
         .unstack(fill_value=0)
         .reset_index())
print (df11)
contact  ID_client l  date l  email  mail  sms  tel
0                  1    2017      2     1    1    0
1                  2    2017      0     0    0    2
df2 = (df.groupby(['ID_client l', df['date l'].dt.strftime('%Y/%m'),'contact'])
         .size()
         .unstack(fill_value=0)
         .reset_index())
print (df2)
contact  ID_client l   date l  email  mail  sms  tel
0                  1  2017/01      2     1    0    0
1                  1  2017/06      0     0    1    0
2                  2  2017/06      0     0    0    2