Change row values with certain condition, using row values from json dataset Pandas










3















I have this dataset.




"date": "2018-01-01",
"body": "some txt",
"id": 111,
"sentiment": null
,

"date": "2018-01-02",
"body": "some txt",
"id": 112,
"sentiment":
"basic": "Bearish"




I want to read this with pandas, and change the column sentiment foreach rows differents from null.



When I do this:



pd.read_json(path)


this is the result that I get:



body ... sentiment
0 None
1 u'basic': u'Bullish'


I don't want to have u'basic': u'Bullish' but only the value of basic.
So to find the correct rows I use



df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = (?)


and it work, but I don't know what I have to put instead of (?)



I've tried this but doens't work



df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = df['sentiment']['basic]


Any ideas? Thanks










share|improve this question




























    3















    I have this dataset.




    "date": "2018-01-01",
    "body": "some txt",
    "id": 111,
    "sentiment": null
    ,

    "date": "2018-01-02",
    "body": "some txt",
    "id": 112,
    "sentiment":
    "basic": "Bearish"




    I want to read this with pandas, and change the column sentiment foreach rows differents from null.



    When I do this:



    pd.read_json(path)


    this is the result that I get:



    body ... sentiment
    0 None
    1 u'basic': u'Bullish'


    I don't want to have u'basic': u'Bullish' but only the value of basic.
    So to find the correct rows I use



    df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = (?)


    and it work, but I don't know what I have to put instead of (?)



    I've tried this but doens't work



    df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = df['sentiment']['basic]


    Any ideas? Thanks










    share|improve this question


























      3












      3








      3








      I have this dataset.




      "date": "2018-01-01",
      "body": "some txt",
      "id": 111,
      "sentiment": null
      ,

      "date": "2018-01-02",
      "body": "some txt",
      "id": 112,
      "sentiment":
      "basic": "Bearish"




      I want to read this with pandas, and change the column sentiment foreach rows differents from null.



      When I do this:



      pd.read_json(path)


      this is the result that I get:



      body ... sentiment
      0 None
      1 u'basic': u'Bullish'


      I don't want to have u'basic': u'Bullish' but only the value of basic.
      So to find the correct rows I use



      df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = (?)


      and it work, but I don't know what I have to put instead of (?)



      I've tried this but doens't work



      df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = df['sentiment']['basic]


      Any ideas? Thanks










      share|improve this question
















      I have this dataset.




      "date": "2018-01-01",
      "body": "some txt",
      "id": 111,
      "sentiment": null
      ,

      "date": "2018-01-02",
      "body": "some txt",
      "id": 112,
      "sentiment":
      "basic": "Bearish"




      I want to read this with pandas, and change the column sentiment foreach rows differents from null.



      When I do this:



      pd.read_json(path)


      this is the result that I get:



      body ... sentiment
      0 None
      1 u'basic': u'Bullish'


      I don't want to have u'basic': u'Bullish' but only the value of basic.
      So to find the correct rows I use



      df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = (?)


      and it work, but I don't know what I have to put instead of (?)



      I've tried this but doens't work



      df.loc[self.df['sentiment'].isnull() != True, 'sentiment'] = df['sentiment']['basic]


      Any ideas? Thanks







      python pandas dataset






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 15:12









      Malik Asad

      316112




      316112










      asked Nov 15 '18 at 12:13









      Aso StrifeAso Strife

      505320




      505320






















          3 Answers
          3






          active

          oldest

          votes


















          3














          You can try:



          mask = df['sentiment'].notnull()
          df.loc[mask, 'sentiment'] = df.loc[mask, 'sentiment'].apply(lambda x: x['basic'])





          share|improve this answer























          • It works. Thanks. This answer will be mark as correct.

            – Aso Strife
            Nov 15 '18 at 12:20


















          2














          You can do this:



          df = pd.read_json(path) # creates the dataframe with dict objects in sentiment column 
          pd.concat([df.drop(['sentiment'], axis=1), df['sentiment'].apply(pd.Series)], axis=1) # create new columns for each sentiment type


          So for example, if your json is :



          [
          "date": "2018-01-01",
          "body": "some txt",
          "id": 111,
          "sentiment": null
          ,

          "date": "2018-01-02",
          "body": "some txt",
          "id": 112,
          "sentiment":
          "basic": "Bearish"

          ,

          "date": "2018-01-03",
          "body": "some other txt",
          "id": 113,
          "sentiment":
          "basic" : "Bullish",
          "non_basic" : "Bearish"

          ]


          df after line 1:



           body date id sentiment
          0 some txt 2018-01-01 111 None
          1 some txt 2018-01-02 112 'basic': 'Bearish'
          2 some other txt 2018-01-03 113 'basic': 'Bullish', 'non_basic': 'Bearish'


          df after line 2:



           body date id basic non_basic
          0 some txt 2018-01-01 111 NaN NaN
          1 some txt 2018-01-02 112 Bearish NaN
          2 some other txt 2018-01-03 113 Bullish Bearish


          HTH.






          share|improve this answer
































            0















            fillna + pop + join



            Here's an extendible solution which avoids row-wise apply and converts an arbitrary number of keys to series:



            df = pd.DataFrame('body': [0, 1],
            'sentiment': [None, u'basic': u'Bullish'])

            df['sentiment'] = df['sentiment'].fillna(pd.Series([]*len(df.index), index=df.index))

            df = df.join(pd.DataFrame(df.pop('sentiment').values.tolist()))

            print(df)

            body basic
            0 0 NaN
            1 1 Bullish





            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',
              autoActivateHeartbeat: false,
              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%2f53319274%2fchange-row-values-with-certain-condition-using-row-values-from-json-dataset-pan%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









              3














              You can try:



              mask = df['sentiment'].notnull()
              df.loc[mask, 'sentiment'] = df.loc[mask, 'sentiment'].apply(lambda x: x['basic'])





              share|improve this answer























              • It works. Thanks. This answer will be mark as correct.

                – Aso Strife
                Nov 15 '18 at 12:20















              3














              You can try:



              mask = df['sentiment'].notnull()
              df.loc[mask, 'sentiment'] = df.loc[mask, 'sentiment'].apply(lambda x: x['basic'])





              share|improve this answer























              • It works. Thanks. This answer will be mark as correct.

                – Aso Strife
                Nov 15 '18 at 12:20













              3












              3








              3







              You can try:



              mask = df['sentiment'].notnull()
              df.loc[mask, 'sentiment'] = df.loc[mask, 'sentiment'].apply(lambda x: x['basic'])





              share|improve this answer













              You can try:



              mask = df['sentiment'].notnull()
              df.loc[mask, 'sentiment'] = df.loc[mask, 'sentiment'].apply(lambda x: x['basic'])






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 12:17









              paulo.filip3paulo.filip3

              2,16811422




              2,16811422












              • It works. Thanks. This answer will be mark as correct.

                – Aso Strife
                Nov 15 '18 at 12:20

















              • It works. Thanks. This answer will be mark as correct.

                – Aso Strife
                Nov 15 '18 at 12:20
















              It works. Thanks. This answer will be mark as correct.

              – Aso Strife
              Nov 15 '18 at 12:20





              It works. Thanks. This answer will be mark as correct.

              – Aso Strife
              Nov 15 '18 at 12:20













              2














              You can do this:



              df = pd.read_json(path) # creates the dataframe with dict objects in sentiment column 
              pd.concat([df.drop(['sentiment'], axis=1), df['sentiment'].apply(pd.Series)], axis=1) # create new columns for each sentiment type


              So for example, if your json is :



              [
              "date": "2018-01-01",
              "body": "some txt",
              "id": 111,
              "sentiment": null
              ,

              "date": "2018-01-02",
              "body": "some txt",
              "id": 112,
              "sentiment":
              "basic": "Bearish"

              ,

              "date": "2018-01-03",
              "body": "some other txt",
              "id": 113,
              "sentiment":
              "basic" : "Bullish",
              "non_basic" : "Bearish"

              ]


              df after line 1:



               body date id sentiment
              0 some txt 2018-01-01 111 None
              1 some txt 2018-01-02 112 'basic': 'Bearish'
              2 some other txt 2018-01-03 113 'basic': 'Bullish', 'non_basic': 'Bearish'


              df after line 2:



               body date id basic non_basic
              0 some txt 2018-01-01 111 NaN NaN
              1 some txt 2018-01-02 112 Bearish NaN
              2 some other txt 2018-01-03 113 Bullish Bearish


              HTH.






              share|improve this answer





























                2














                You can do this:



                df = pd.read_json(path) # creates the dataframe with dict objects in sentiment column 
                pd.concat([df.drop(['sentiment'], axis=1), df['sentiment'].apply(pd.Series)], axis=1) # create new columns for each sentiment type


                So for example, if your json is :



                [
                "date": "2018-01-01",
                "body": "some txt",
                "id": 111,
                "sentiment": null
                ,

                "date": "2018-01-02",
                "body": "some txt",
                "id": 112,
                "sentiment":
                "basic": "Bearish"

                ,

                "date": "2018-01-03",
                "body": "some other txt",
                "id": 113,
                "sentiment":
                "basic" : "Bullish",
                "non_basic" : "Bearish"

                ]


                df after line 1:



                 body date id sentiment
                0 some txt 2018-01-01 111 None
                1 some txt 2018-01-02 112 'basic': 'Bearish'
                2 some other txt 2018-01-03 113 'basic': 'Bullish', 'non_basic': 'Bearish'


                df after line 2:



                 body date id basic non_basic
                0 some txt 2018-01-01 111 NaN NaN
                1 some txt 2018-01-02 112 Bearish NaN
                2 some other txt 2018-01-03 113 Bullish Bearish


                HTH.






                share|improve this answer



























                  2












                  2








                  2







                  You can do this:



                  df = pd.read_json(path) # creates the dataframe with dict objects in sentiment column 
                  pd.concat([df.drop(['sentiment'], axis=1), df['sentiment'].apply(pd.Series)], axis=1) # create new columns for each sentiment type


                  So for example, if your json is :



                  [
                  "date": "2018-01-01",
                  "body": "some txt",
                  "id": 111,
                  "sentiment": null
                  ,

                  "date": "2018-01-02",
                  "body": "some txt",
                  "id": 112,
                  "sentiment":
                  "basic": "Bearish"

                  ,

                  "date": "2018-01-03",
                  "body": "some other txt",
                  "id": 113,
                  "sentiment":
                  "basic" : "Bullish",
                  "non_basic" : "Bearish"

                  ]


                  df after line 1:



                   body date id sentiment
                  0 some txt 2018-01-01 111 None
                  1 some txt 2018-01-02 112 'basic': 'Bearish'
                  2 some other txt 2018-01-03 113 'basic': 'Bullish', 'non_basic': 'Bearish'


                  df after line 2:



                   body date id basic non_basic
                  0 some txt 2018-01-01 111 NaN NaN
                  1 some txt 2018-01-02 112 Bearish NaN
                  2 some other txt 2018-01-03 113 Bullish Bearish


                  HTH.






                  share|improve this answer















                  You can do this:



                  df = pd.read_json(path) # creates the dataframe with dict objects in sentiment column 
                  pd.concat([df.drop(['sentiment'], axis=1), df['sentiment'].apply(pd.Series)], axis=1) # create new columns for each sentiment type


                  So for example, if your json is :



                  [
                  "date": "2018-01-01",
                  "body": "some txt",
                  "id": 111,
                  "sentiment": null
                  ,

                  "date": "2018-01-02",
                  "body": "some txt",
                  "id": 112,
                  "sentiment":
                  "basic": "Bearish"

                  ,

                  "date": "2018-01-03",
                  "body": "some other txt",
                  "id": 113,
                  "sentiment":
                  "basic" : "Bullish",
                  "non_basic" : "Bearish"

                  ]


                  df after line 1:



                   body date id sentiment
                  0 some txt 2018-01-01 111 None
                  1 some txt 2018-01-02 112 'basic': 'Bearish'
                  2 some other txt 2018-01-03 113 'basic': 'Bullish', 'non_basic': 'Bearish'


                  df after line 2:



                   body date id basic non_basic
                  0 some txt 2018-01-01 111 NaN NaN
                  1 some txt 2018-01-02 112 Bearish NaN
                  2 some other txt 2018-01-03 113 Bullish Bearish


                  HTH.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 12:27

























                  answered Nov 15 '18 at 12:22









                  Deepak SainiDeepak Saini

                  1,609816




                  1,609816





















                      0















                      fillna + pop + join



                      Here's an extendible solution which avoids row-wise apply and converts an arbitrary number of keys to series:



                      df = pd.DataFrame('body': [0, 1],
                      'sentiment': [None, u'basic': u'Bullish'])

                      df['sentiment'] = df['sentiment'].fillna(pd.Series([]*len(df.index), index=df.index))

                      df = df.join(pd.DataFrame(df.pop('sentiment').values.tolist()))

                      print(df)

                      body basic
                      0 0 NaN
                      1 1 Bullish





                      share|improve this answer



























                        0















                        fillna + pop + join



                        Here's an extendible solution which avoids row-wise apply and converts an arbitrary number of keys to series:



                        df = pd.DataFrame('body': [0, 1],
                        'sentiment': [None, u'basic': u'Bullish'])

                        df['sentiment'] = df['sentiment'].fillna(pd.Series([]*len(df.index), index=df.index))

                        df = df.join(pd.DataFrame(df.pop('sentiment').values.tolist()))

                        print(df)

                        body basic
                        0 0 NaN
                        1 1 Bullish





                        share|improve this answer

























                          0












                          0








                          0








                          fillna + pop + join



                          Here's an extendible solution which avoids row-wise apply and converts an arbitrary number of keys to series:



                          df = pd.DataFrame('body': [0, 1],
                          'sentiment': [None, u'basic': u'Bullish'])

                          df['sentiment'] = df['sentiment'].fillna(pd.Series([]*len(df.index), index=df.index))

                          df = df.join(pd.DataFrame(df.pop('sentiment').values.tolist()))

                          print(df)

                          body basic
                          0 0 NaN
                          1 1 Bullish





                          share|improve this answer














                          fillna + pop + join



                          Here's an extendible solution which avoids row-wise apply and converts an arbitrary number of keys to series:



                          df = pd.DataFrame('body': [0, 1],
                          'sentiment': [None, u'basic': u'Bullish'])

                          df['sentiment'] = df['sentiment'].fillna(pd.Series([]*len(df.index), index=df.index))

                          df = df.join(pd.DataFrame(df.pop('sentiment').values.tolist()))

                          print(df)

                          body basic
                          0 0 NaN
                          1 1 Bullish






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 12:24









                          jppjpp

                          102k2165115




                          102k2165115



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53319274%2fchange-row-values-with-certain-condition-using-row-values-from-json-dataset-pan%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







                              這個網誌中的熱門文章

                              Barbados

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3