我正在研究 Python 中的資料框。我有 10 天的原始資料框。我每天都劃分了該資料框并嘗試繪制。我在某些列中有一些奇怪的值(這里是 y 和 z),所以我嘗試使用“介于方法”來指定我的范圍(0,100)。代碼正在運行,但我收到警告。誰能幫幫我?
for df in ((listofDF)):
if len(df) != 0:
f_df = df[df[' y'].between(0,100)]
f_df = f_df[df[' z'].between(0,100)]
maxTemp = f_df[' y']
minTemp = f_df[' z']
Time = f_df['x']
plt.plot(x,y)
plt.plot(x,z)
我得到的警告是,UserWarning: Boolean Series key will be reindexed to match DataFrame index. f_df = f_df[df['y'].between(0,100)]
uj5u.com熱心網友回復:
TL;DR 解決方案
更改f_df = f_df[df[' z'].between(0, 100)]為f_df = f_df[f_df[' z'].between(0, 100)]
您收到的警告是因為這條線:
f_df = f_df[df[' z'].between(0,100)]
這條線有問題,你能發現嗎?
您正在使用df索引f_df。你在這里基本上做的是獲取行df,列z在 0 到 100 之間,所以我們假設 df 是第 2 行和第 4 行。
但是,在 f_df 中,行可能完全不同。這意味著在 f_df (這是一個不同的資料幀)中,z介于 0 和 100 之間的行是第 3 行和第 10 行。因為您在這個意義上使用df索引f_df(因為您正在獲得滿足條件的索引df并使用這些索引從f_df) 中選擇行,pandas 告訴您f_df' 的索參考于決定保留哪些行,這可能不是您想要的。
因此,當您對過濾器進行過濾df并回傳第 1 行和第 10 行時,它會從中選擇第 1 行和第 10 行f_df。或者更準確地說-它將選擇索引 1 和 10。
在您的情況下,這是您想要的,因為在您創建資料框時會保留索引f_df,正如您列印出來時左側的索引所看到的那樣。
>>> df = pd.DataFrame([('a', 1, 51), ('b', 51, 31)], columns=['letter', 'x', 'y'])
>>> f_df = df[df.x.between(0, 50)]
>>> f_df
letter x y
0 a 1 51
>>> f_df = f_df[df.y.between(0, 50)]
<stdin>:1: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
>>> f_df
Empty DataFrame
Columns: [letter, x, y]
Index: []
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/439405.html
上一篇:統計過去14天中特定值的發生次數
下一篇:如何在R中制作排名列
