我需要在Bidfloor
列上插入特定值,但我的问题是当我运行df_g['Bidfloor'] = df_g[['Sitio', 'Country']].merge(df_seg, how='left').Precio
Bidfloor
列时,取值为NaN而不是floorprice
的值我对df_g['Bidfloor'] = df_g[['Sitio', 'Espacio', 'Country']].merge(df_seg, how='left').Precio
floorprice = 0.17
df_g = pd.read_csv('este_mes.csv')
df_g = df_g[df_g.Subastas > 1000]
df_g.to_csv('aaaa.csv')
df_seg = pd.read_csv('output.csv', names=['Espacio', 'Country', 'Precio', 'Sitio'])
df_g['Bidfloor'] = floorprice
df_g['Bidfloor'] = df_g[['Sitio', 'Country']].merge(df_seg, how='left').Precio
df_g['Bidfloor'] = df_g[['Sitio', 'Espacio', 'Country']].merge(df_seg, how='left').Precio
df_g.to_csv('Analizador_{}.csv'.format(auth), index=False)
输出:
Sitio,Espacio,Tamano,Country,Impresiones_exchange,Importe_a_cobrar,eCPM,Subastas,Fill_rate,Bidfloor
A,3619717 - www.A.com.ar - Seccion - Seccion300x250B,300x250,DE - Germany,846,0.21,0.25,1312,64.48,0.1
B,3619717 - www.A.com.ar - Seccion - Seccion300x250B,300x250,AR - Argentina,846,0.21,0.25,1312,64.48,NaN
输出我需要:
Sitio,Espacio,Tamano,Country,Impresiones_exchange,Importe_a_cobrar,eCPM,Subastas,Fill_rate,Bidfloor
A,3619717 - www.A.com.ar - Seccion - Seccion300x250B,300x250,DE - Germany,846,0.21,0.25,1312,64.48,0.1
B,3619717 - www.A.com.ar - Seccion - Seccion300x250B,300x250,AR - Argentina,846,0.21,0.25,1312,64.48,0.2
df_g:
Sitio,Espacio,Tamano,Country,Impresiones_exchange,Importe_a_cobrar,eCPM,Subastas,Fill_rate
A,3619717 - www.A.com.ar - Seccion - Seccion300x250B,300x250,DE - Germany,846,0.21,0.25,1312,64.48
B,3619717 - www.A.com.ar - Seccion - Seccion300x250B,300x250,AR - Argentina,846,0.21,0.25,1312,64.48
df_seg:
Espacio,Country,Precio,Sitio
3619717 - www.A.com.ar - Seccion - Seccion300x250B,DE - Germany,0.1,A
*,AR - Argentina,0.2,A
答案 0 :(得分:0)
我认为你的根本问题是你的合并声明不够具体。如Pandas merge documentation says所示,如果您没有为on
参数添加任何值,并且还保留left_index
和right_index
参数false
,那么默认情况下,pandas默认合并在列的交集上。这意味着如果来自每个公共列的值相同,它只会加入行。
在您的示例中,由于Sitio
列,您的合并不会为第二行返回任何内容。在df_g
中,第二行的值为' B'对于此列,在df_seg
中,第二行的值为' A':
Here是我用于此处的废码的链接(对不起,如果它看起来有些奇怪,Gists似乎并不能很好地处理Jupyter笔记本)。