Why can't you replace integers with lists using `replace` method - pandas











up vote
5
down vote

favorite












So let's say i have a pandas data-frame as below:



df=pd.DataFrame({'a':[1,2,3,0]})


So my goal is to replace 0 value with (empty list) in this data-frame, but i did:



print(df.replace(0,))


But it gives me an error:



TypeError: Invalid "to_replace" type: 'int'


I tried everything that's possible i.e:



df[df==0]=


etc...



But nothing works.



Desired output (in case for confusion):



   a
0 1
1 2
2 3
3









share|improve this question




















  • 1




    It does the trick with print(df.replace(0,""))
    – pygo
    2 days ago










  • Amm... i want a real list that is append-able...
    – U9-Forward
    2 days ago






  • 1




    because df.a is of type int. You need it to be of type object. e.g. try initialize df with df=pd.DataFrame({'a':[1,2,3,]}) and compare then df.a.dtype
    – SpghttCd
    2 days ago










  • @SpghttCd Thanks for suggestion, and i'll try.
    – U9-Forward
    2 days ago






  • 1




    @U9-Forward The method suggested by SpghttCd with loc works check df=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a'])).
    – Sandeep Kadapa
    2 days ago















up vote
5
down vote

favorite












So let's say i have a pandas data-frame as below:



df=pd.DataFrame({'a':[1,2,3,0]})


So my goal is to replace 0 value with (empty list) in this data-frame, but i did:



print(df.replace(0,))


But it gives me an error:



TypeError: Invalid "to_replace" type: 'int'


I tried everything that's possible i.e:



df[df==0]=


etc...



But nothing works.



Desired output (in case for confusion):



   a
0 1
1 2
2 3
3









share|improve this question




















  • 1




    It does the trick with print(df.replace(0,""))
    – pygo
    2 days ago










  • Amm... i want a real list that is append-able...
    – U9-Forward
    2 days ago






  • 1




    because df.a is of type int. You need it to be of type object. e.g. try initialize df with df=pd.DataFrame({'a':[1,2,3,]}) and compare then df.a.dtype
    – SpghttCd
    2 days ago










  • @SpghttCd Thanks for suggestion, and i'll try.
    – U9-Forward
    2 days ago






  • 1




    @U9-Forward The method suggested by SpghttCd with loc works check df=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a'])).
    – Sandeep Kadapa
    2 days ago













up vote
5
down vote

favorite









up vote
5
down vote

favorite











So let's say i have a pandas data-frame as below:



df=pd.DataFrame({'a':[1,2,3,0]})


So my goal is to replace 0 value with (empty list) in this data-frame, but i did:



print(df.replace(0,))


But it gives me an error:



TypeError: Invalid "to_replace" type: 'int'


I tried everything that's possible i.e:



df[df==0]=


etc...



But nothing works.



Desired output (in case for confusion):



   a
0 1
1 2
2 3
3









share|improve this question















So let's say i have a pandas data-frame as below:



df=pd.DataFrame({'a':[1,2,3,0]})


So my goal is to replace 0 value with (empty list) in this data-frame, but i did:



print(df.replace(0,))


But it gives me an error:



TypeError: Invalid "to_replace" type: 'int'


I tried everything that's possible i.e:



df[df==0]=


etc...



But nothing works.



Desired output (in case for confusion):



   a
0 1
1 2
2 3
3






python pandas dataframe replace types






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago

























asked 2 days ago









U9-Forward

9,9682834




9,9682834








  • 1




    It does the trick with print(df.replace(0,""))
    – pygo
    2 days ago










  • Amm... i want a real list that is append-able...
    – U9-Forward
    2 days ago






  • 1




    because df.a is of type int. You need it to be of type object. e.g. try initialize df with df=pd.DataFrame({'a':[1,2,3,]}) and compare then df.a.dtype
    – SpghttCd
    2 days ago










  • @SpghttCd Thanks for suggestion, and i'll try.
    – U9-Forward
    2 days ago






  • 1




    @U9-Forward The method suggested by SpghttCd with loc works check df=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a'])).
    – Sandeep Kadapa
    2 days ago














  • 1




    It does the trick with print(df.replace(0,""))
    – pygo
    2 days ago










  • Amm... i want a real list that is append-able...
    – U9-Forward
    2 days ago






  • 1




    because df.a is of type int. You need it to be of type object. e.g. try initialize df with df=pd.DataFrame({'a':[1,2,3,]}) and compare then df.a.dtype
    – SpghttCd
    2 days ago










  • @SpghttCd Thanks for suggestion, and i'll try.
    – U9-Forward
    2 days ago






  • 1




    @U9-Forward The method suggested by SpghttCd with loc works check df=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a'])).
    – Sandeep Kadapa
    2 days ago








1




1




It does the trick with print(df.replace(0,""))
– pygo
2 days ago




It does the trick with print(df.replace(0,""))
– pygo
2 days ago












Amm... i want a real list that is append-able...
– U9-Forward
2 days ago




Amm... i want a real list that is append-able...
– U9-Forward
2 days ago




1




1




because df.a is of type int. You need it to be of type object. e.g. try initialize df with df=pd.DataFrame({'a':[1,2,3,]}) and compare then df.a.dtype
– SpghttCd
2 days ago




because df.a is of type int. You need it to be of type object. e.g. try initialize df with df=pd.DataFrame({'a':[1,2,3,]}) and compare then df.a.dtype
– SpghttCd
2 days ago












@SpghttCd Thanks for suggestion, and i'll try.
– U9-Forward
2 days ago




@SpghttCd Thanks for suggestion, and i'll try.
– U9-Forward
2 days ago




1




1




@U9-Forward The method suggested by SpghttCd with loc works check df=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a'])).
– Sandeep Kadapa
2 days ago




@U9-Forward The method suggested by SpghttCd with loc works check df=pd.DataFrame({'a':[1,2,3,0]},dtype='str');df.loc[3,'a'] = ;print(type(df.loc[3,'a'])).
– Sandeep Kadapa
2 days ago












3 Answers
3






active

oldest

votes

















up vote
3
down vote



accepted










It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:



df['a'] = [ if x == 0 else x for x in df.a]

print (df)

a
0 1
1 2
2 3
3


And replace all values in all columns:



df = df.applymap(lambda x:  if x == 0 else x)
print (df)
a
0 1
1 2
2 3
3





share|improve this answer



















  • 1




    Note that [] * len(df) may not work if len(df) > 1. It will create a list containing the same instance of empty list rather than len(df) unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
    – Lie Ryan
    2 days ago








  • 1




    Thanks so much, i appreciate it, came back and accepted this!!!
    – U9-Forward
    yesterday






  • 1




    @U9-Forward - Thank you very much!
    – jezrael
    yesterday


















up vote
1
down vote













There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;



df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]


This creates n empty list based on the number of items that matches the criteria (df == 0)



The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.



df = df.astype(object)
df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]





share|improve this answer




























    up vote
    -1
    down vote













    This works for me:



    df=pd.DataFrame({'a':[1,8,3,0]})
    print(df)

    a
    0 1
    1 8
    2 3
    3 0

    df['a'] = df['a'].apply(lambda d: d if d!=0 else )
    print(df)

    a
    0 1
    1 8
    2 3
    3





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410163%2fwhy-cant-you-replace-integers-with-lists-using-replace-method-pandas%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:



      df['a'] = [ if x == 0 else x for x in df.a]

      print (df)

      a
      0 1
      1 2
      2 3
      3


      And replace all values in all columns:



      df = df.applymap(lambda x:  if x == 0 else x)
      print (df)
      a
      0 1
      1 2
      2 3
      3





      share|improve this answer



















      • 1




        Note that [] * len(df) may not work if len(df) > 1. It will create a list containing the same instance of empty list rather than len(df) unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
        – Lie Ryan
        2 days ago








      • 1




        Thanks so much, i appreciate it, came back and accepted this!!!
        – U9-Forward
        yesterday






      • 1




        @U9-Forward - Thank you very much!
        – jezrael
        yesterday















      up vote
      3
      down vote



      accepted










      It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:



      df['a'] = [ if x == 0 else x for x in df.a]

      print (df)

      a
      0 1
      1 2
      2 3
      3


      And replace all values in all columns:



      df = df.applymap(lambda x:  if x == 0 else x)
      print (df)
      a
      0 1
      1 2
      2 3
      3





      share|improve this answer



















      • 1




        Note that [] * len(df) may not work if len(df) > 1. It will create a list containing the same instance of empty list rather than len(df) unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
        – Lie Ryan
        2 days ago








      • 1




        Thanks so much, i appreciate it, came back and accepted this!!!
        – U9-Forward
        yesterday






      • 1




        @U9-Forward - Thank you very much!
        – jezrael
        yesterday













      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:



      df['a'] = [ if x == 0 else x for x in df.a]

      print (df)

      a
      0 1
      1 2
      2 3
      3


      And replace all values in all columns:



      df = df.applymap(lambda x:  if x == 0 else x)
      print (df)
      a
      0 1
      1 2
      2 3
      3





      share|improve this answer














      It is possible by list comprehension, but because mixed content - numeric with list it is not recommended:



      df['a'] = [ if x == 0 else x for x in df.a]

      print (df)

      a
      0 1
      1 2
      2 3
      3


      And replace all values in all columns:



      df = df.applymap(lambda x:  if x == 0 else x)
      print (df)
      a
      0 1
      1 2
      2 3
      3






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 2 days ago

























      answered 2 days ago









      jezrael

      308k20245319




      308k20245319








      • 1




        Note that [] * len(df) may not work if len(df) > 1. It will create a list containing the same instance of empty list rather than len(df) unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
        – Lie Ryan
        2 days ago








      • 1




        Thanks so much, i appreciate it, came back and accepted this!!!
        – U9-Forward
        yesterday






      • 1




        @U9-Forward - Thank you very much!
        – jezrael
        yesterday














      • 1




        Note that [] * len(df) may not work if len(df) > 1. It will create a list containing the same instance of empty list rather than len(df) unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
        – Lie Ryan
        2 days ago








      • 1




        Thanks so much, i appreciate it, came back and accepted this!!!
        – U9-Forward
        yesterday






      • 1




        @U9-Forward - Thank you very much!
        – jezrael
        yesterday








      1




      1




      Note that [] * len(df) may not work if len(df) > 1. It will create a list containing the same instance of empty list rather than len(df) unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
      – Lie Ryan
      2 days ago






      Note that [] * len(df) may not work if len(df) > 1. It will create a list containing the same instance of empty list rather than len(df) unique empty lists, which is probably not what you wanted. You'll need to use list comprehension to create new lists.
      – Lie Ryan
      2 days ago






      1




      1




      Thanks so much, i appreciate it, came back and accepted this!!!
      – U9-Forward
      yesterday




      Thanks so much, i appreciate it, came back and accepted this!!!
      – U9-Forward
      yesterday




      1




      1




      @U9-Forward - Thank you very much!
      – jezrael
      yesterday




      @U9-Forward - Thank you very much!
      – jezrael
      yesterday












      up vote
      1
      down vote













      There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;



      df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]


      This creates n empty list based on the number of items that matches the criteria (df == 0)



      The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.



      df = df.astype(object)
      df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]





      share|improve this answer

























        up vote
        1
        down vote













        There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;



        df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]


        This creates n empty list based on the number of items that matches the criteria (df == 0)



        The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.



        df = df.astype(object)
        df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;



          df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]


          This creates n empty list based on the number of items that matches the criteria (df == 0)



          The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.



          df = df.astype(object)
          df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]





          share|improve this answer












          There are two issues here. First is pandas' quirkiness when dealing with lists. To replace values in a DataFrame with list you need to do something like this;



          df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]


          This creates n empty list based on the number of items that matches the criteria (df == 0)



          The second issue is that your column is of integer type, and you can't store a list in an integer column. So before you can assign the list, you would first need to convert the column type to object first.



          df = df.astype(object)
          df.loc[df.a == 0, "a"] = [ for _ in df[df.a == 0]]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Lie Ryan

          43.8k868121




          43.8k868121






















              up vote
              -1
              down vote













              This works for me:



              df=pd.DataFrame({'a':[1,8,3,0]})
              print(df)

              a
              0 1
              1 8
              2 3
              3 0

              df['a'] = df['a'].apply(lambda d: d if d!=0 else )
              print(df)

              a
              0 1
              1 8
              2 3
              3





              share|improve this answer

























                up vote
                -1
                down vote













                This works for me:



                df=pd.DataFrame({'a':[1,8,3,0]})
                print(df)

                a
                0 1
                1 8
                2 3
                3 0

                df['a'] = df['a'].apply(lambda d: d if d!=0 else )
                print(df)

                a
                0 1
                1 8
                2 3
                3





                share|improve this answer























                  up vote
                  -1
                  down vote










                  up vote
                  -1
                  down vote









                  This works for me:



                  df=pd.DataFrame({'a':[1,8,3,0]})
                  print(df)

                  a
                  0 1
                  1 8
                  2 3
                  3 0

                  df['a'] = df['a'].apply(lambda d: d if d!=0 else )
                  print(df)

                  a
                  0 1
                  1 8
                  2 3
                  3





                  share|improve this answer












                  This works for me:



                  df=pd.DataFrame({'a':[1,8,3,0]})
                  print(df)

                  a
                  0 1
                  1 8
                  2 3
                  3 0

                  df['a'] = df['a'].apply(lambda d: d if d!=0 else )
                  print(df)

                  a
                  0 1
                  1 8
                  2 3
                  3






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  nixon

                  83116




                  83116






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410163%2fwhy-cant-you-replace-integers-with-lists-using-replace-method-pandas%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Contact image not getting when fetch all contact list from iPhone by CNContact

                      count number of partitions of a set with n elements into k subsets

                      A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks