从熊猫中的数据透视表中删除空值和切片



我在切片透视数据框时遇到问题,以便获得高于特定阈值的结果。我正在尝试过滤掉低于最小值的结果。我的数据框如下所示:

              Qty                             
Index   Store_Nbr   201712  201801  201802  201803  201804  201805  201806  201807  201808
0               1      356     275     293     256     313     421     493     291     385
1               2      147     316     343     416     361     483     438     136     461
2               3      266     370     162     346     451     414     296     478     295
3               4      322     179     353     241     370     247     423     391     194
4               5      249     389     480     450     102     482     137     251     153
...            ...     ...     ...     ...     ...     ...     ...     ...     ...     ...
30             30        0       0       0       0       0       0       0       0       0
31             31        0       0       0       0       0       0       0       0       0
32             32        0       0       0       0       0       0       0       0       0
33             33      392     311     151     488     135     239     212     104     122
34             34        0       0       0       0       0       0       0       0      -1

使用godzilla = godzilla[godzilla['Qty'] > 150]后,我得到了下面的数据框,它将所有零转换为空值,并且没有过滤掉任何内容。

               Qty                                
Index   Store_Nbr   201712   201801  201802  201803  201804  201805  201806  201807  201808
0             NaN      NaN      275     293     256     313     421     493     291     385
1             NaN      147      316     343     416     361     483     438     136     461
2             NaN      266      370     162     346     451     414     296     478     295
3             NaN      NaN      179     353     241     370     247     423     391     194
4             NaN      389      480     450     102     482     137     251     153     153
...           ...     ...       ...     ...     ...     ...     ...     ...     ...     ...
30            NaN      NaN      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
31            NaN      NaN      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
32            NaN      NaN      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
33            NaN      NaN      311     151     488     135     239     212     104     122
34            NaN      NaN      NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN

我尝试做godzilla.dropna(axis = 0, inplace = True, how = 'any')它返回一个空数据帧,godzilla = godzilla.dropna( subset = godzilla['Qty'])返回一个键错误:"数量"。我很困惑它在空值中转换了零以及为什么切片不起作用。在尝试过滤/切片透视数据时有什么智慧的话吗?

注意** 我在数据框中旋转的不仅仅是数量。

是的!您正在使用分层结构、多索引。这就是为什么列没有被删除,而是替换为NaN.如果除Qty之外没有其他索引,则可以使用:

godzilla.columns = godzilla.columns.droplevel()

然后跟着dropna()

godzilla = godzilla.dropna(how='any',axis=0)

首先,将小于 150 的每个值替换为 None。
您应该过滤掉至少具有 1 None 值的行,如本答案中所述

这应该可以解决问题:

# replaces every value of the DataFrame lower than 150 with None
godzilla = pd.DataFrame([
[x if not isinstance(x, (int, float)) or x >= 150 else None for x in j
] for j in godzilla.as_matrix()])
# replaces every value of the Column "Qty" lower than 150 with None
df["Qty"] = [x if not isinstance(x, (int, float)) or x >= 20 else None for x in df["Qty"]
#dropna will drop all rows with at least 1 null value
df = df.dropna(how='any',axis=0)

我终于想通了。抱歉耽搁了。感谢大家提出的答案。错误完全在我身上。在代码的前面,我使用了.drop_index(),因为当时我没有意识到我可以使用.index来操作索引。更正代码后,使用godzilla = godzilla[godzilla['Qty'] > 150].dropna()设置参数没有问题。

最新更新