我有一个包含多列的数据框,我只想将 "Views" 列的最大值替换为基于特定条件的三个不同列。
import pandas as PD
data = [["1.Blend Of Vdx Display","DISPLAY","Features","CPE",1255,778732,13373,7142],["1.Blend Of Vdx Display","DISPLAY","TVC","CPE",10479,778732,13373,7142],
["2.Mobile VDX","Display","Features","CPE",168,1000,150,160],["2.Mobile VDX","Display","Features","CPE",2309,1000,150,160]]
df = pd.DataFrame(data,columns=['Placement#Name','PRODUCT','VIDEONAME','COST_TYPE',views','IMPRESSIONS','ENGAGEMENTS','DPEENGAMENTS'])
print(df)
Placement#Name PRODUCT VIDEONAME COST_TYPE views IMPRESSIONS \
0 1.Blend Of Vdx Display DISPLAY Features CPE 1255 778732
1 1.Blend Of Vdx Display DISPLAY TVC CPE 10479 778732
2 2.Mobile VDX DISPLAY Features CPE 168 1000
3 2.Mobile VDX DISPLAY Features CPE 2309 1000
ENGAGEMENTS DPEENGAMENTS
0 13373 7142
1 13373 7142
2 150 160
3 150 160
我可以通过这样做过滤掉最大值
newdf = df.loc[df.reset_index().groupby(['Placement#Name'])['Views'].idxmax()]
print (newdf)
Placement#Name PRODUCT VIDEONAME COST_TYPE Views IMPRESSIONS \
1 1.Blend Of Vdx Display DISPLAY TVC CPE 10479 778732
3 2.Mobile VDX DISPLAY Features CPE 2309 1000
ENGAGEMENTS DPEENGAMENTS
1 13373 7142
3 150 160
现在我想用 10479 和 2309 等条件替换 newdf View ,可以用 Engagements 列替换,因为条件是 Product is display 且 Cost_Type 是 CPE。
所以新的 df 输出是
print (newdf)
Placement#Name PRODUCT VIDEONAME COST_TYPE Views IMPRESSIONS \
1 1.Blend Of Vdx Display DISPLAY TVC CPE 13373 778732
3 2.Mobile VDX DISPLAY Features CPE 150 1000
ENGAGEMENTS DPEENGAMENTS
1 13373 7142
3 150 160
然后我想在原始 df 上转换它。 所以原始输出是:
print (df)
Placement#Name PRODUCT VIDEONAME COST_TYPE views IMPRESSIONS \
0 1.Blend Of Vdx Display DISPLAY Features CPE 1255 778732
1 1.Blend Of Vdx Display DISPLAY TVC CPE 13373 778732
2 2.Mobile VDX DISPLAY Features CPE 168 1000
3 2.Mobile VDX DISPLAY Features CPE 150 1000
ENGAGEMENTS DPEENGAMENTS
0 13373 7142
1 13373 7142
2 150 160
3 150 160
请您参考如下方法:
另一种方法 -
这会过滤掉并替换从 views 到 ENGAGEMENTS
newdf['views'] = newdf.apply(lambda x: x['ENGAGEMENTS'] if ((x['PRODUCT'].upper()=='DISPLAY') & (x['COST_TYPE']=='CPE')) else x['views'], axis=1)
附加 2 个数据帧
df['views'].update(newdf['views'])
输出
Placement#Name PRODUCT VIDEONAME COST_TYPE views IMPRESSIONS \
0 1.Blend Of Vdx Display DISPLAY Features CPE 1255 778732
1 1.Blend Of Vdx Display DISPLAY TVC CPE 13373 778732
2 2.Mobile VDX Display Features CPE 168 1000
3 2.Mobile VDX Display Features CPE 150 1000
ENGAGEMENTS DPEENGAMENTS
0 13373 7142
1 13373 7142
2 150 160
3 150 160
