【问题标题】:How to solve pandas dataframe column is not recognizing its value如何解决 pandas 数据框列无法识别其价值
【发布时间】:2020-06-10 14:57:34
【问题描述】:

我正在使用 Kaggle Spotify 1921-2020 (data.csv) 数据集,其中包含 object 数据类型的列“艺术家”,我尝试对数据框 spotify[spotify['artists'] == 'Dennis Day'] 执行条件提取,它不输出任何行无论我输入什么值都会发生。我试图将其数据类型更改为strspotify.artists.apply(str),但仍然没有发生任何事情,它的数据类型仍然显示为object

到目前为止我做了什么

# previously the value was like
0                                       ['Dennis Day']
1    ['Sergei Rachmaninoff', 'James Levine', 'Berli...


# I remove the square braces
spotify['artists'] = spotify['artists'].apply(lambda x : x[1:-1])

0                                         'Dennis Day'
1    'Sergei Rachmaninoff', 'James Levine', 'Berlin...

# since it was giving no output for any value I checked this
for i in songs.artists.head(10):
    print(f'{i} is present {i in songs.artists}')

# and see what I get

'Dennis Day' is present False
'Sergei Rachmaninoff', 'James Levine', 'Berliner Philharmoniker' is present False
'John McCormack' is present False

# don't get confused if somewhere it is written songs and instead of spotify they are same.

我尝试将 Kaggle notebook 更改为 jupyterlab,但它都一样,所以我现在该怎么办

【问题讨论】:

标签: python-3.x pandas


【解决方案1】:

对于数据框的条件提取,请尝试使用.loc

spotify.loc[spotify['artists'] == 'Dennis Day']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-13
    • 2021-04-29
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2019-09-11
    相关资源
    最近更新 更多