For loop based on a if statement, error message= The truth value of a Series is ambiguous [duplicate] Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!Conditional Replace Pandasdropping rows from dataframe based on a “not in” conditionCheck if string is in a pandas dataframeDataframe.isin() giving this error: The truth value of a DataFrame is ambiguousThe truth value of a Series is ambiguousif else function in pandas dataframeValueError in pandas: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()ValueAccessing data frames with multiple condition applied on columns giving error. Error: The truth value of a Series is ambiguouspandas add columns ,note The truth value of a Series is ambiguousGetting error in python as “**The truth value of a Series is ambiguous”Apply if statement groupby pandas dataframe. The truth value of a Series is ambiguous error
What is the meaning of the new sigil in Game of Thrones Season 8 intro?
How to call a function with default parameter through a pointer to function that is the return of another function?
Why didn't this character "real die" when they blew their stack out in Altered Carbon?
What does an IRS interview request entail when called in to verify expenses for a sole proprietor small business?
What to do with chalk when deepwater soloing?
Why aren't air breathing engines used as small first stages
How do pianists reach extremely loud dynamics?
List *all* the tuples!
Identify plant with long narrow paired leaves and reddish stems
When a candle burns, why does the top of wick glow if bottom of flame is hottest?
How do I keep my slimes from escaping their pens?
Apollo command module space walk?
Why was the term "discrete" used in discrete logarithm?
Should I use a zero-interest credit card for a large one-time purchase?
How does debian/ubuntu knows a package has a updated version
Identifying polygons that intersect with another layer using QGIS?
Why light coming from distant stars is not discreet?
Seeking colloquialism for “just because”
Generate an RGB colour grid
Can a USB port passively 'listen only'?
What does this icon in iOS Stardew Valley mean?
Extract all GPU name, model and GPU ram
How come Sam didn't become Lord of Horn Hill?
What causes the vertical darker bands in my photo?
For loop based on a if statement, error message= The truth value of a Series is ambiguous [duplicate]
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!Conditional Replace Pandasdropping rows from dataframe based on a “not in” conditionCheck if string is in a pandas dataframeDataframe.isin() giving this error: The truth value of a DataFrame is ambiguousThe truth value of a Series is ambiguousif else function in pandas dataframeValueError in pandas: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()ValueAccessing data frames with multiple condition applied on columns giving error. Error: The truth value of a Series is ambiguouspandas add columns ,note The truth value of a Series is ambiguousGetting error in python as “**The truth value of a Series is ambiguous”Apply if statement groupby pandas dataframe. The truth value of a Series is ambiguous error
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Conditional Replace Pandas
5 answers
My dataframe looks like this:
mid price dse_high_born
0 0.002039 False
1 0.002039 False
2 0.002039 False
3 0.002039 False
4 0.002039 False
5 0.002038 False
6 0.002039 True
7 0.002037 False
8 0.002037 False
9 0.002037 False
10 0.002036 False
11 0.002036 False
12 0.002038 False
13 0.002038 False
14 0.002038 False
15 0.002038 False
16 0.002039 False
17 0.002039 False
18 0.002040 False
19 0.002040 False
20 0.002040 False
21 0.002039 False
22 0.002039 False
23 0.002039 False
24 0.002040 True
25 0.002040 False
26 0.002041 False
27 0.002041 False
28 0.002041 False
29 0.002042 False
30 0.002044 False
31 0.002049 True
32 0.002049 False
33 0.002048 False
... ...
I tried to use a for loop to add a new column price based on a condition as followed:
for index, row in df.iterrows():
if df['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
I received the following error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I tried every combination (with bool(), any(), item(), etc) but when I do the following request df[df['price'] != 'nan'] there is nothing in my dataframe with this condition, any idea why? thanks!
python pandas
marked as duplicate by jezrael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 9:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Conditional Replace Pandas
5 answers
My dataframe looks like this:
mid price dse_high_born
0 0.002039 False
1 0.002039 False
2 0.002039 False
3 0.002039 False
4 0.002039 False
5 0.002038 False
6 0.002039 True
7 0.002037 False
8 0.002037 False
9 0.002037 False
10 0.002036 False
11 0.002036 False
12 0.002038 False
13 0.002038 False
14 0.002038 False
15 0.002038 False
16 0.002039 False
17 0.002039 False
18 0.002040 False
19 0.002040 False
20 0.002040 False
21 0.002039 False
22 0.002039 False
23 0.002039 False
24 0.002040 True
25 0.002040 False
26 0.002041 False
27 0.002041 False
28 0.002041 False
29 0.002042 False
30 0.002044 False
31 0.002049 True
32 0.002049 False
33 0.002048 False
... ...
I tried to use a for loop to add a new column price based on a condition as followed:
for index, row in df.iterrows():
if df['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
I received the following error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I tried every combination (with bool(), any(), item(), etc) but when I do the following request df[df['price'] != 'nan'] there is nothing in my dataframe with this condition, any idea why? thanks!
python pandas
marked as duplicate by jezrael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 9:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Conditional Replace Pandas
5 answers
My dataframe looks like this:
mid price dse_high_born
0 0.002039 False
1 0.002039 False
2 0.002039 False
3 0.002039 False
4 0.002039 False
5 0.002038 False
6 0.002039 True
7 0.002037 False
8 0.002037 False
9 0.002037 False
10 0.002036 False
11 0.002036 False
12 0.002038 False
13 0.002038 False
14 0.002038 False
15 0.002038 False
16 0.002039 False
17 0.002039 False
18 0.002040 False
19 0.002040 False
20 0.002040 False
21 0.002039 False
22 0.002039 False
23 0.002039 False
24 0.002040 True
25 0.002040 False
26 0.002041 False
27 0.002041 False
28 0.002041 False
29 0.002042 False
30 0.002044 False
31 0.002049 True
32 0.002049 False
33 0.002048 False
... ...
I tried to use a for loop to add a new column price based on a condition as followed:
for index, row in df.iterrows():
if df['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
I received the following error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I tried every combination (with bool(), any(), item(), etc) but when I do the following request df[df['price'] != 'nan'] there is nothing in my dataframe with this condition, any idea why? thanks!
python pandas
This question already has an answer here:
Conditional Replace Pandas
5 answers
My dataframe looks like this:
mid price dse_high_born
0 0.002039 False
1 0.002039 False
2 0.002039 False
3 0.002039 False
4 0.002039 False
5 0.002038 False
6 0.002039 True
7 0.002037 False
8 0.002037 False
9 0.002037 False
10 0.002036 False
11 0.002036 False
12 0.002038 False
13 0.002038 False
14 0.002038 False
15 0.002038 False
16 0.002039 False
17 0.002039 False
18 0.002040 False
19 0.002040 False
20 0.002040 False
21 0.002039 False
22 0.002039 False
23 0.002039 False
24 0.002040 True
25 0.002040 False
26 0.002041 False
27 0.002041 False
28 0.002041 False
29 0.002042 False
30 0.002044 False
31 0.002049 True
32 0.002049 False
33 0.002048 False
... ...
I tried to use a for loop to add a new column price based on a condition as followed:
for index, row in df.iterrows():
if df['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
I received the following error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I tried every combination (with bool(), any(), item(), etc) but when I do the following request df[df['price'] != 'nan'] there is nothing in my dataframe with this condition, any idea why? thanks!
This question already has an answer here:
Conditional Replace Pandas
5 answers
python pandas
python pandas
asked Mar 22 at 9:09
Viktor.wViktor.w
3889
3889
marked as duplicate by jezrael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 9:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jezrael
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 22 at 9:15
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
This can be done in a much more simple and efficient way using np.where:
import numpy as np
df['price'] = np.where(df.dse_high_born, df.mid_price, np.nan)
mid_price dse_high_born price
0 0.002 False NaN
1 0.002 False NaN
2 0.002 False NaN
3 0.002 False NaN
4 0.002 False NaN
5 0.002 False NaN
6 0.002 True 0.002
7 0.002 False NaN
...
The problem with your code, is that in the if statement, when checking the condition df['dse_high_born'] == True:, you are not indexing on a particular row, but on the entire colum. You need to index both on row and column using .loc, df.loc[index,'dse_high_born']. So you want something like:
for index, row in df.iterrows():
if df.loc[index,'dse_high_born'] == True:
df.loc[index,'price'] = df.loc[index,'mid_price']
else:
df.loc[index,'price'] = np.nan
add a comment |
The error is refering to df['dse_high_born'] == True. I think it should be replaced by row like this?
for index, row in df.iterrows():
if row['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can be done in a much more simple and efficient way using np.where:
import numpy as np
df['price'] = np.where(df.dse_high_born, df.mid_price, np.nan)
mid_price dse_high_born price
0 0.002 False NaN
1 0.002 False NaN
2 0.002 False NaN
3 0.002 False NaN
4 0.002 False NaN
5 0.002 False NaN
6 0.002 True 0.002
7 0.002 False NaN
...
The problem with your code, is that in the if statement, when checking the condition df['dse_high_born'] == True:, you are not indexing on a particular row, but on the entire colum. You need to index both on row and column using .loc, df.loc[index,'dse_high_born']. So you want something like:
for index, row in df.iterrows():
if df.loc[index,'dse_high_born'] == True:
df.loc[index,'price'] = df.loc[index,'mid_price']
else:
df.loc[index,'price'] = np.nan
add a comment |
This can be done in a much more simple and efficient way using np.where:
import numpy as np
df['price'] = np.where(df.dse_high_born, df.mid_price, np.nan)
mid_price dse_high_born price
0 0.002 False NaN
1 0.002 False NaN
2 0.002 False NaN
3 0.002 False NaN
4 0.002 False NaN
5 0.002 False NaN
6 0.002 True 0.002
7 0.002 False NaN
...
The problem with your code, is that in the if statement, when checking the condition df['dse_high_born'] == True:, you are not indexing on a particular row, but on the entire colum. You need to index both on row and column using .loc, df.loc[index,'dse_high_born']. So you want something like:
for index, row in df.iterrows():
if df.loc[index,'dse_high_born'] == True:
df.loc[index,'price'] = df.loc[index,'mid_price']
else:
df.loc[index,'price'] = np.nan
add a comment |
This can be done in a much more simple and efficient way using np.where:
import numpy as np
df['price'] = np.where(df.dse_high_born, df.mid_price, np.nan)
mid_price dse_high_born price
0 0.002 False NaN
1 0.002 False NaN
2 0.002 False NaN
3 0.002 False NaN
4 0.002 False NaN
5 0.002 False NaN
6 0.002 True 0.002
7 0.002 False NaN
...
The problem with your code, is that in the if statement, when checking the condition df['dse_high_born'] == True:, you are not indexing on a particular row, but on the entire colum. You need to index both on row and column using .loc, df.loc[index,'dse_high_born']. So you want something like:
for index, row in df.iterrows():
if df.loc[index,'dse_high_born'] == True:
df.loc[index,'price'] = df.loc[index,'mid_price']
else:
df.loc[index,'price'] = np.nan
This can be done in a much more simple and efficient way using np.where:
import numpy as np
df['price'] = np.where(df.dse_high_born, df.mid_price, np.nan)
mid_price dse_high_born price
0 0.002 False NaN
1 0.002 False NaN
2 0.002 False NaN
3 0.002 False NaN
4 0.002 False NaN
5 0.002 False NaN
6 0.002 True 0.002
7 0.002 False NaN
...
The problem with your code, is that in the if statement, when checking the condition df['dse_high_born'] == True:, you are not indexing on a particular row, but on the entire colum. You need to index both on row and column using .loc, df.loc[index,'dse_high_born']. So you want something like:
for index, row in df.iterrows():
if df.loc[index,'dse_high_born'] == True:
df.loc[index,'price'] = df.loc[index,'mid_price']
else:
df.loc[index,'price'] = np.nan
edited Mar 22 at 9:17
answered Mar 22 at 9:12
yatuyatu
16k41642
16k41642
add a comment |
add a comment |
The error is refering to df['dse_high_born'] == True. I think it should be replaced by row like this?
for index, row in df.iterrows():
if row['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
add a comment |
The error is refering to df['dse_high_born'] == True. I think it should be replaced by row like this?
for index, row in df.iterrows():
if row['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
add a comment |
The error is refering to df['dse_high_born'] == True. I think it should be replaced by row like this?
for index, row in df.iterrows():
if row['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
The error is refering to df['dse_high_born'] == True. I think it should be replaced by row like this?
for index, row in df.iterrows():
if row['dse_high_born'] == True:
df.at[index,'price'] = row['mid price']
else:
df.at[index,'price'] = 'nan'
answered Mar 22 at 9:12
erncyperncyp
326311
326311
add a comment |
add a comment |