我在 CSV 中讀取了 python 中的數據框。我有一個 DateTimeIndex 和我感興趣的兩列,我們稱它們為 number 和 upper_limit。我按索引排序,刪除屬于舊時間戳的不必要的列和行。然后我計算這兩列的最小值、最大值和平均值numbercol = pd.to_numeric(df.iloc[:,0], errors='coerce')upperlimitcol = pd.to_numeric(df.iloc[:,1], errors = 'coerce')這工作正?!,F在我想檢查數字大于 upper_limit 的頻率for dt in df.index: if numbercol[dt] >= upperlimitcol[dt]: overshoots += 1但我得到一個ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().我添加了一個打印語句來查看每個的 number 和 upper_limit 列的值,dt結果證明在 1800 行之后,單元格中的值不再是數字,但看起來像這樣(這就是它給我的print(numbercol[dt]))DateTime2017-01-14 NaN2017-01-14 3018.0Name: Number, dtype: float64的類型numbercol[dt]也從<type 'numpy.float64'>到<class 'pandas.core.series.Series'>我在文本編輯器以及 Libre Office 和 Excel 中檢查了該文件,但看不出此行與之前的行之間有任何區別。你知道為什么會這樣嗎?
1 回答

慕運維8079593
TA貢獻1876條經驗 獲得超5個贊
它正在返回一個系列,因為您有兩個具有相同dt. 不知道問題的背景,很難說如何進行。
一種方式是聚集在該數據用于使用循環sum()或一些其它aggregrating功能(即max(),min()等):
for dt in df.index:
if numbercol[dt].sum() >= upperlimitcol[dt]:
overshoots += 1
另一個可能是在 for 循環之前 dropna() 。
numbercol = numbercol.dropna()
添加回答
舉報
0/150
提交
取消