ElinaElizabeth 发表于 2020-10-24 15:40:30

请帮我看看这段pandas代码为什么有错?

import pandas as pd
from datetime import datetime

df = pd.read_excel('bank-index-daily.xlsx',parse_dates=['date'])   #date列读取为日期。

df['year'] = df.date.dt.year #新设一列,标签为year,从日期列截取信息写入这一列。
df['month'] = df.date.dt.month

#筛选year=2007,0<month<8,对银行业涨跌幅升序排序,输出最小的前7个
df.loc[(df['year'] == '2007'& (0<df['month']<8)) ,['date','jump-ratio','year','month']].sort_index(['jump-rato']).head(7)

ElinaElizabeth 发表于 2020-10-24 15:54:34

报错的内容:
Traceback (most recent call last):
File "C:\Users\swift\Desktop\standard MES.py", line 12, in <module>
    df.loc[(df['year'] == '2007') & (0<df['month']<8) ,['date','jump-ratio','year','month']].sort_index(['jump-rato']).head(7)
File "C:\Users\swift\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 1329, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

疾风怪盗 发表于 2020-10-24 17:16:55

假如我们想要筛选D列数据中大于0的行:df>0]
筛选用错了,loc是选择值了,要么series要么具体值
页: [1]
查看完整版本: 请帮我看看这段pandas代码为什么有错?