I'm noob with pandas, and recently, I got that 'ValueError' when I'm trying to modify the columns that follows some rules, as:
我是大熊猫的noob,最近,当我尝试修改遵循某些规则的列时,我得到了'ValueError',如:
csv_input = pd.read_csv(fn, error_bad_lines=False)
if csv_input['ip.src'] == '192.168.1.100':
csv_input['flow_dir'] = 1
csv_input['ip.src'] = 1
csv_input['ip.dst'] = 0
else:
if csv_input['ip.dst'] == '192.168.1.100':
csv_input['flow_dir'] = 0
csv_input['ip.src'] = 0
csv_input['ip.dst'] = 1
I was searching about this error and I guess that it's because the 'if' statement and the '==' operator, but I don't know how to fix this.
我正在搜索这个错误,我想这是因为'if'语句和'=='运算符,但我不知道如何解决这个问题。
Thanks!
1 个解决方案
#1
0
So Andrew L's comment is correct, but I'm going to expand on it a bit for your benefit.
所以安德鲁的评论是正确的,但我会为了你的利益而扩展它。
When you call, e.g.
当你打电话时,例如
csv_input['ip.dst'] == '192.168.1.100'
What this returns is a Series, with the same index as csv_input, but all the values in that series are boolean, and represent whether the value in csv_input['ip.dst']
for that row is equal to '192.168.1.100'
.
返回的是一个Series,其索引与csv_input相同,但该系列中的所有值都是boolean,并表示该行的csv_input ['ip.dst']中的值是否等于'192.168.1.100'。
So, when you call
所以,当你打电话的时候
if csv_input['ip.dst'] == '192.168.1.100':
You're asking whether that Series evaluates to True or False. Hopefully that explains what it meant by The truth value of a Series is ambiguous.
, it's a Series, it can't be boiled down to a boolean.
你问这个系列是评估为True还是False。希望这能解释它的含义。系列的真值是模棱两可的。它是一个系列,它不能归结为布尔值。
Now, what it looks like you're trying to do is set the values in the flow_dir
,ip.src
& ip.dst
columns, based on the value in the ip.src
column.
现在,您尝试做的是根据ip.src列中的值设置flow_dir,ip.src和ip.dst列中的值。
The correct way to do this is would be with .loc[]
, something like this:
执行此操作的正确方法是.loc [],如下所示:
#equivalent to first if statement
csv_input.loc[
csv_input['ip.src'] = '192.168.1.100',
('ip.src','ip.dst','flow_dir')
] = (1,0,1)
#equivalent to second if statement
csv_input.loc[
csv_input['ip.dst'] = '192.168.1.100',
('ip.src','ip.dst','flow_dir')
] = (0,1,0)
#1
0
So Andrew L's comment is correct, but I'm going to expand on it a bit for your benefit.
所以安德鲁的评论是正确的,但我会为了你的利益而扩展它。
When you call, e.g.
当你打电话时,例如
csv_input['ip.dst'] == '192.168.1.100'
What this returns is a Series, with the same index as csv_input, but all the values in that series are boolean, and represent whether the value in csv_input['ip.dst']
for that row is equal to '192.168.1.100'
.
返回的是一个Series,其索引与csv_input相同,但该系列中的所有值都是boolean,并表示该行的csv_input ['ip.dst']中的值是否等于'192.168.1.100'。
So, when you call
所以,当你打电话的时候
if csv_input['ip.dst'] == '192.168.1.100':
You're asking whether that Series evaluates to True or False. Hopefully that explains what it meant by The truth value of a Series is ambiguous.
, it's a Series, it can't be boiled down to a boolean.
你问这个系列是评估为True还是False。希望这能解释它的含义。系列的真值是模棱两可的。它是一个系列,它不能归结为布尔值。
Now, what it looks like you're trying to do is set the values in the flow_dir
,ip.src
& ip.dst
columns, based on the value in the ip.src
column.
现在,您尝试做的是根据ip.src列中的值设置flow_dir,ip.src和ip.dst列中的值。
The correct way to do this is would be with .loc[]
, something like this:
执行此操作的正确方法是.loc [],如下所示:
#equivalent to first if statement
csv_input.loc[
csv_input['ip.src'] = '192.168.1.100',
('ip.src','ip.dst','flow_dir')
] = (1,0,1)
#equivalent to second if statement
csv_input.loc[
csv_input['ip.dst'] = '192.168.1.100',
('ip.src','ip.dst','flow_dir')
] = (0,1,0)