Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Last active April 16, 2021 03:04
Show Gist options
  • Save thanakijwanavit/199cc690d5920cfd9066cca129d5eb93 to your computer and use it in GitHub Desktop.
Save thanakijwanavit/199cc690d5920cfd9066cca129d5eb93 to your computer and use it in GitHub Desktop.
convert string list to list in dataframe
# for a list that looks like this
'''
“[strawberry, apple, orange]”
[credit](https://towardsdatascience.com/dealing-with-list-values-in-pandas-dataframes-a177e534f173)
'''
def clean_alt_list(list_):
list_ = list_.replace(', ', '","')
list_ = list_.replace('[', '["')
list_ = list_.replace(']', '"]')
return list_
df['col'].apply(clean_al_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment