无法选择Pandas

2024-05-16 04:32:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我以前也做过多次同样的事情,但是我无法基于数据帧中的列值选择行。我也尝试将此列作为索引,但没有成功。我只能查询第一行。 这是数据帧:

| PLAYER   | Pts

|0 | SunilNarine | 379.5|

|1 |Shane Watson | 318.0|

df[df.PLAYER=='SunilNarine']

很好用。 其他任何记录都是一样的

df[df.PLAYER=='Shane Watson']

什么都不给。 我也试着把这个列作为索引,只对第一个记录有效。 也尝试过:

for player in df['PLAYER']:
    if str(player).strip().capitalize=='Shane Watson'.capitalize:
        print('Y')

它什么也没印出来。你知道吗

我有多张唱片,这里我只介绍了两张。除了第一行之外,它无法基于PLAYER列选择任何行。适用于其他列。我想不出这里出了什么问题。你知道吗


Tags: 数据indfforif记录watson事情
2条回答

请尝试以下代码:

# Import pandas library 
  import pandas as pd 
  data = [['SunilNarine', 379.5], ['Shane Watson', 318.0], ['Virat Kohli', 543]] 
  df = pd.DataFrame(data, columns = ['PLAYER', 'Pts']) 

  # print the records
  print(df[df.PLAYER=='Virat Kohli'])
  print(df[df.PLAYER=='Shane Watson'])
  print(df[df.Pts== 379.5])

Please refer the Image

[unidecode.unidecode(x) for x in df['PLAYER']]

对于删除所有特殊字符效果很好。列中有许多“/xa”和“/n”字符。你知道吗

相关问题 更多 >