我有一个看起来像这样的Pandas DataFrame:
Date Channel Sessions
0 1/1/2018 Branded Paid Search 1057
1 1/1/2018 Direct Traffic 4039
2 1/1/2018 Display 474
3 1/1/2018 Email 801
4 1/1/2018 Generic Paid Search 195
5 1/1/2018 Organic Search 6617
6 1/1/2018 Referral 563
7 1/1/2018 Social 7752
8 1/2/2018 Branded Paid Search 2172
9 1/2/2018 Direct Traffic 10444
10 1/2/2018 Display 613
11 1/2/2018 Email 1674
12 1/2/2018 Generic Paid Search 291
13 1/2/2018 Organic Search 14752
14 1/2/2018 Referral 1412
15 1/2/2018 Social 7858
16 1/3/2018 Branded Paid Search 2150
17 1/3/2018 Direct Traffic 9883
18 1/3/2018 Display 1201
19 1/3/2018 Email 2575
20 1/3/2018 Generic Paid Search 284
21 1/3/2018 Organic Search 15424
22 1/3/2018 Referral 2122
23 1/3/2018 Social 8513
实际df大得多。
我想要的是将某个频道的某个日期范围内的值强制为零。 IE:在01/03/2018和11/03/2018之间的所有品牌付费搜索为零[0]
如果我将日期和频道分组(并以日期作为索引),则可以进行以下操作:sessions_df.loc[startdate:enddate, 'Branded Paid Search'] = 0
但是,出于我要达到的目的,我需要保持上述格式。我尝试了以下方法:
df.loc[(df['Date'] >= startdate) & (df['Date'] <= enddate) & (df['Channel'] == 'Branded Paid Search')]['Sessions'] = 0
但是我遇到以下错误:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
我已经尝试了以下[部分有效],但是它将整个行强制为零:
df.loc[(df['Date'] >= startdate) & (df['Date'] <= enddate) & (df['Channel'] == 'Branded Paid Search')] = 0
关于如何将这些值强制设为零的任何想法?