我已经成功过滤了列值,但是对于该数据帧,过滤器在过滤器之后返回了NaN数据帧。我不确定我哪里错了。我正在发布带有结果的代码。

import pandas as pd
df = pd.read_csv("http://portal.amfiindia.com/DownloadNAVHistoryReport_Po.aspx?mf=17&tp=1&frmdt=04-Nov-2017&todt=02-Dec-2018",sep=";",parse_dates=['Date'])
df=df.drop(['Repurchase Price','Sale Price'],axis=1)
df = df.dropna()
df['Net Asset Value'] = df['Net Asset Value'].apply(pd.to_numeric,errors='coerce')
df.columns = [['scheme_code','scheme','nav','date']]
df[df['scheme_code'] == '123690']


过滤的结果是

      scheme_code scheme                  nav date
2          123690    NaN                  nan  NaT
3          123690    NaN                  nan  NaT
4          123690    NaN                  nan  NaT
5          123690    NaN                  nan  NaT
6          123690    NaN                  nan  NaT
7          123690    NaN                  nan  NaT
8          123690    NaN                  nan  NaT
9          123690    NaN                  nan  NaT
10         123690    NaN                  nan  NaT
11         123690    NaN                  nan  NaT
12         123690    NaN                  nan  NaT
13         123690    NaN                  nan  NaT
14         123690    NaN                  nan  NaT
15         123690    NaN                  nan  NaT
16         123690    NaN                  nan  NaT
17         123690    NaN                  nan  NaT
18         123690    NaN                  nan  NaT
19         123690    NaN                  nan  NaT
20         123690    NaN                  nan  NaT
21         123690    NaN                  nan  NaT
22         123690    NaN                  nan  NaT
23         123690    NaN                  nan  NaT
24         123690    NaN                  nan  NaT
25         123690    NaN                  nan  NaT
26         123690    NaN                  nan  NaT
27         123690    NaN                  nan  NaT
28         123690    NaN                  nan  NaT
29         123690    NaN                  nan  NaT
30         123690    NaN                  nan  NaT
31         123690    NaN                  nan  NaT


但是,如果我看一下dataframe head方法,我可以看到带有值的实际数据

  scheme_code                               scheme                  nav  \
2      123690  Kotak Banking and PSU Debt - Growth                38.60
3      123690  Kotak Banking and PSU Debt - Growth                38.58
4      123690  Kotak Banking and PSU Debt - Growth                38.58
5      123690  Kotak Banking and PSU Debt - Growth                38.59
6      123690  Kotak Banking and PSU Debt - Growth                38.59

        date
2 2017-11-06
3 2017-11-07
4 2017-11-08
5 2017-11-09


我也尝试转换为数字,但仍然可以看到相同的结果。如果有人可以帮助我找出错误所在,我将不胜感激。

最佳答案

问题是您将列指定为列表的列表(请注意双括号),因此不满足条件。只需将其更改为简单列表:

df.columns = ['scheme_code','scheme','nav','date']

关于python - Pandas 不基于列值进行过滤,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53663629/

10-16 22:55