【发布时间】:2021-08-12 05:11:31
【问题描述】:
我想为Customer_Acquisition_Channel 列中的每个类别添加Days_To_Acquisition 列中的所有值到单独的df。
所有 Customer_ID 值在下面的数据集中都是唯一的
DF
Customer_ID Customer_Acquisition_Channel Days_To_Acquisition
323 Organic 2
583 Organic 5
838 Organic 2
193 Website 7
241 Website 7
642 Website 1
期望的输出: Days_To_Acq_Organic_Df
Index Days_To_Acquisition
0 2
1 5
2 2
Days_To_Acq_Website_Df
Index Days_To_Acquisition
0 7
1 7
2 1
这是我迄今为止尝试过的,但我想使用 for 循环而不是手动遍历每一列
sub_1 = df.loc[df['Customer_Acquisition_Channel'] == 'Organic']
Days_To_Acq_Organic_Df=sub_1[['Days_To_Acquisition']]
sub_2 = df.loc[df['Customer_Acquisition_Channel'] == 'Website']
Days_To_Acq_Website_Df=sub_2[['Days_To_Acquisition']]
【问题讨论】:
-
我不确定您要对结果做什么,但我认为
pd.groupby可以提供帮助并与agg或apply结合使用或不使用lambda函数或即使有一个列表理解也可以帮助您从所需的结果中获得更多。 -
df_dict = {f'Days_To_Acquisition_{g}_df':k.drop('Customer_Acquisition_Channel', 1) for g,k in df.groupby('Customer_Acquisition_Channel')}??