这是我的数据集:
offerid category countty browserid devid click
0 887235 17714 e Firefox NaN 0
1 178235 21407 b Mozilla Desktop 0
2 518539 25085 a Edge NaN 0
3 390352 40339 c Firefox Mobile 0
4 472937 12052 d Mozilla Desktop 0
每次Firefox出现在'browserid'列中时,我需要用'Mobile'填充'devid'列中的'NaN'。这是我为实现这一目标而编写的代码。
if data['browserid'] == 'Firefox' :
data['devid'].fillna('Mobile', inplace=True)
我遇到以下错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
任何帮助都会得到帮助!
答案 0 :(得分:1)
from io import StringIO
import pandas as pd
TESTDATA=StringIO(""" offerid category countty browserid devid click
0 887235 17714 e Firefox NaN 0
1 178235 21407 b Mozilla Desktop 0
2 518539 25085 a Edge NaN 0
3 390352 40339 c Firefox Mobile 0
4 472937 12052 d Mozilla Desktop 0""")
df = pd.read_csv(TESTDATA, sep=" ")
df = df.iloc[:,1:]
df.loc[df['browserid'] == 'Firefox','devid'] = df.loc[df['browserid'] == 'Firefox','devid'].fillna('Mobile')
答案 1 :(得分:0)
此外,你可以这样做:
data.loc[data.browserid == "Firefox",'devid'] = 0 (or "NaN")
但问题的答案是你用一个字符串比较一个列......
答案 2 :(得分:0)
offerid category countty browserid devid click
0 887235 17714 e Firefox Mobile 0
1 178235 21407 b Mozilla Desktop 0
2 518539 25085 a Edge NaN 0
3 390352 40339 c Firefox Mobile 0
4 472937 12052 d Mozilla NaN 0
Read data using pandas
import pandas as pd
df = pd.read_csv('stack_overflow.csv')
df.loc[(df['browserid']=='Firefox')&(df.devid.isnull()),'devid'] ='Mobile'
output
offerid category countty browserid devid click
0 887235 17714 e Firefox Mobile 0
1 178235 21407 b Mozilla Desktop 0
2 518539 25085 a Edge NaN 0
3 390352 40339 c Firefox Mobile 0
4 472937 12052 d Mozilla NaN 0