python - 在 Pandas 中获取值(value)作为列表

标签 python pandas

我的数据框有一列类型列表,它看起来像这样:

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

我正在使用 iterrows() 迭代数据帧,但是当我得到列值时它是一个字符串,我如何才能作为列表加载?

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

最佳答案

使用ast.literal_eval :

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

关于python - 在 Pandas 中获取值(value)作为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53137988/

相关文章:

python - 我的 csv 中有多个列。如何使用 python 将行值与引用列匹配?

python - 如何禁用输入输入直到双击?

python - Pandas 数据帧图 : lists of values

python - 基于另一列 bool 值的累积和

python - 对 Django 的 Ajax 调用发送电子邮件没有响应

Python子进程的cpu使用率随机下降到0%,导致进程为 "hang up"

python - 使用 scipy 最大化目标(通过 kelly criterium)

python - Pandas - 仅删除等于零的连续行

python - 如何用 Pandas 构建矢量化函数?

python - 如何从 Python Pandas 系列或数据框中的行中删除省略号,当长行/宽列被截断时显示?