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
python pandas
add a comment |
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
python pandas
expected output?
– pygo
Nov 4 at 5:25
what aboutdf["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
@bakkarow['Genres']
returns a string, even if I saved a list in that dataframe
– Hohenheimsenberg
Nov 4 at 5:42
add a comment |
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
python pandas
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
python pandas
asked Nov 4 at 5:23
Hohenheimsenberg
460416
460416
expected output?
– pygo
Nov 4 at 5:25
what aboutdf["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
@bakkarow['Genres']
returns a string, even if I saved a list in that dataframe
– Hohenheimsenberg
Nov 4 at 5:42
add a comment |
expected output?
– pygo
Nov 4 at 5:25
what aboutdf["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
@bakkarow['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
add a comment |
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)
add a comment |
up vote
2
down vote
Use eval to convert a string that is actually a list to list
artist_genres = eval(row['Genres'])
add a comment |
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)
add a comment |
up vote
4
down vote
accepted
Use ast.literal_eval
:
import ast
df['Genre'] = df['Genre'].apply(ast.literal_eval)
add a comment |
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)
Use ast.literal_eval
:
import ast
df['Genre'] = df['Genre'].apply(ast.literal_eval)
edited Nov 8 at 6:54
answered Nov 4 at 5:25
Sandeep Kadapa
5,519427
5,519427
add a comment |
add a comment |
up vote
2
down vote
Use eval to convert a string that is actually a list to list
artist_genres = eval(row['Genres'])
add a comment |
up vote
2
down vote
Use eval to convert a string that is actually a list to list
artist_genres = eval(row['Genres'])
add a comment |
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'])
Use eval to convert a string that is actually a list to list
artist_genres = eval(row['Genres'])
answered Nov 4 at 5:26
Vishnudev
938317
938317
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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