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;








0
















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!










share|improve this question













marked as duplicate by jezrael pandas
Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

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.
























    0
















    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!










    share|improve this question













    marked as duplicate by jezrael pandas
    Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

    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.




















      0












      0








      0









      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!










      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 9:09









      Viktor.wViktor.w

      3889




      3889




      marked as duplicate by jezrael pandas
      Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

      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 pandas
      Users with the  pandas badge can single-handedly close pandas questions as duplicates and reopen them as needed.

      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.
























          2 Answers
          2






          active

          oldest

          votes


















          3














          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





          share|improve this answer
































            1














            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'





            share|improve this answer





























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              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





              share|improve this answer





























                3














                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





                share|improve this answer



























                  3












                  3








                  3







                  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





                  share|improve this answer















                  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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 22 at 9:17

























                  answered Mar 22 at 9:12









                  yatuyatu

                  16k41642




                  16k41642























                      1














                      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'





                      share|improve this answer



























                        1














                        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'





                        share|improve this answer

























                          1












                          1








                          1







                          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'





                          share|improve this answer













                          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'






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 22 at 9:12









                          erncyperncyp

                          326311




                          326311













                              Popular posts from this blog

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                              155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해