Get value as list in pandas











up vote
2
down vote

favorite












My dataframe has one colum of type list, it looks something like this:



Genre                                              Band
['deep pop r&b', 'indie r&b', 'r&b', 'trap soul'], Elijah Blake


I'm iterating the dataframe using iterrows(), but when I get the column value it is a string, how can I can load as a list?



for i, row in df.iterrows():
artist_genres = row['Genres'] #this is a string
print(artist_genres)
for artist_genre in artist_genres:
print(artist_genre) #this prints each character, I want to iterate each genre









share|improve this question






















  • expected output?
    – pygo
    Nov 4 at 5:25












  • what about df["Genres"].tolist()
    – pygo
    Nov 4 at 5:32










  • artist_genres should be a list, how did you get it as a string?
    – bakka
    Nov 4 at 5:37










  • @pygo tolist() returns a list of each character ['[', ''', 'd', ...]
    – Hohenheimsenberg
    Nov 4 at 5:41












  • @bakka row['Genres'] returns a string, even if I saved a list in that dataframe
    – Hohenheimsenberg
    Nov 4 at 5:42

















up vote
2
down vote

favorite












My dataframe has one colum of type list, it looks something like this:



Genre                                              Band
['deep pop r&b', 'indie r&b', 'r&b', 'trap soul'], Elijah Blake


I'm iterating the dataframe using iterrows(), but when I get the column value it is a string, how can I can load as a list?



for i, row in df.iterrows():
artist_genres = row['Genres'] #this is a string
print(artist_genres)
for artist_genre in artist_genres:
print(artist_genre) #this prints each character, I want to iterate each genre









share|improve this question






















  • expected output?
    – pygo
    Nov 4 at 5:25












  • what about df["Genres"].tolist()
    – pygo
    Nov 4 at 5:32










  • artist_genres should be a list, how did you get it as a string?
    – bakka
    Nov 4 at 5:37










  • @pygo tolist() returns a list of each character ['[', ''', 'd', ...]
    – Hohenheimsenberg
    Nov 4 at 5:41












  • @bakka row['Genres'] returns a string, even if I saved a list in that dataframe
    – Hohenheimsenberg
    Nov 4 at 5:42















up vote
2
down vote

favorite









up vote
2
down vote

favorite











My dataframe has one colum of type list, it looks something like this:



Genre                                              Band
['deep pop r&b', 'indie r&b', 'r&b', 'trap soul'], Elijah Blake


I'm iterating the dataframe using iterrows(), but when I get the column value it is a string, how can I can load as a list?



for i, row in df.iterrows():
artist_genres = row['Genres'] #this is a string
print(artist_genres)
for artist_genre in artist_genres:
print(artist_genre) #this prints each character, I want to iterate each genre









share|improve this question













My dataframe has one colum of type list, it looks something like this:



Genre                                              Band
['deep pop r&b', 'indie r&b', 'r&b', 'trap soul'], Elijah Blake


I'm iterating the dataframe using iterrows(), but when I get the column value it is a string, how can I can load as a list?



for i, row in df.iterrows():
artist_genres = row['Genres'] #this is a string
print(artist_genres)
for artist_genre in artist_genres:
print(artist_genre) #this prints each character, I want to iterate each genre






python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 4 at 5:23









Hohenheimsenberg

460416




460416












  • expected output?
    – pygo
    Nov 4 at 5:25












  • what about df["Genres"].tolist()
    – pygo
    Nov 4 at 5:32










  • artist_genres should be a list, how did you get it as a string?
    – bakka
    Nov 4 at 5:37










  • @pygo tolist() returns a list of each character ['[', ''', 'd', ...]
    – Hohenheimsenberg
    Nov 4 at 5:41












  • @bakka row['Genres'] returns a string, even if I saved a list in that dataframe
    – Hohenheimsenberg
    Nov 4 at 5:42




















  • expected output?
    – pygo
    Nov 4 at 5:25












  • what about df["Genres"].tolist()
    – pygo
    Nov 4 at 5:32










  • artist_genres should be a list, how did you get it as a string?
    – bakka
    Nov 4 at 5:37










  • @pygo tolist() returns a list of each character ['[', ''', 'd', ...]
    – Hohenheimsenberg
    Nov 4 at 5:41












  • @bakka row['Genres'] returns a string, even if I saved a list in that dataframe
    – Hohenheimsenberg
    Nov 4 at 5:42


















expected output?
– pygo
Nov 4 at 5:25






expected output?
– pygo
Nov 4 at 5:25














what about df["Genres"].tolist()
– pygo
Nov 4 at 5:32




what about df["Genres"].tolist()
– pygo
Nov 4 at 5:32












artist_genres should be a list, how did you get it as a string?
– bakka
Nov 4 at 5:37




artist_genres should be a list, how did you get it as a string?
– bakka
Nov 4 at 5:37












@pygo tolist() returns a list of each character ['[', ''', 'd', ...]
– Hohenheimsenberg
Nov 4 at 5:41






@pygo tolist() returns a list of each character ['[', ''', 'd', ...]
– Hohenheimsenberg
Nov 4 at 5:41














@bakka row['Genres'] returns a string, even if I saved a list in that dataframe
– Hohenheimsenberg
Nov 4 at 5:42






@bakka row['Genres'] returns a string, even if I saved a list in that dataframe
– Hohenheimsenberg
Nov 4 at 5:42














2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










Use ast.literal_eval:



import ast
df['Genre'] = df['Genre'].apply(ast.literal_eval)





share|improve this answer






























    up vote
    2
    down vote













    Use eval to convert a string that is actually a list to list



    artist_genres = eval(row['Genres'])





    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%2f53137988%2fget-value-as-list-in-pandas%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      Use ast.literal_eval:



      import ast
      df['Genre'] = df['Genre'].apply(ast.literal_eval)





      share|improve this answer



























        up vote
        4
        down vote



        accepted










        Use ast.literal_eval:



        import ast
        df['Genre'] = df['Genre'].apply(ast.literal_eval)





        share|improve this answer

























          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          Use ast.literal_eval:



          import ast
          df['Genre'] = df['Genre'].apply(ast.literal_eval)





          share|improve this answer














          Use ast.literal_eval:



          import ast
          df['Genre'] = df['Genre'].apply(ast.literal_eval)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 8 at 6:54

























          answered Nov 4 at 5:25









          Sandeep Kadapa

          5,519427




          5,519427
























              up vote
              2
              down vote













              Use eval to convert a string that is actually a list to list



              artist_genres = eval(row['Genres'])





              share|improve this answer

























                up vote
                2
                down vote













                Use eval to convert a string that is actually a list to list



                artist_genres = eval(row['Genres'])





                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Use eval to convert a string that is actually a list to list



                  artist_genres = eval(row['Genres'])





                  share|improve this answer












                  Use eval to convert a string that is actually a list to list



                  artist_genres = eval(row['Genres'])






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 4 at 5:26









                  Vishnudev

                  938317




                  938317






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53137988%2fget-value-as-list-in-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







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini