简体   繁体   English

Pandas 删除几列不是nan的行

[英]Pandas remove rows where several columns are not nan

I have a dataframe that looks like this:我有一个看起来像这样的 dataframe:

   A   B   C    D    E
0  P  10 NaN  5.0  9.0
1  Q  19 NaN  NaN  4.0
2  R   8 NaN  3.0  7.0
3  S  20 NaN  3.0  7.0
4  T   4 NaN  2.0  NaN

And I have a list: [['A', 'B', 'D', 'E'], ['A', 'B', 'D'], ['A', 'B', 'E']]我有一个列表: [['A', 'B', 'D', 'E'], ['A', 'B', 'D'], ['A', 'B', 'E']]

I am iterating over the list and getting only those rows from the dataframe, for which the columns specified by the list are not empty.我正在遍历列表并仅从 dataframe 中获取那些行,其中列表指定的列不为空。

I have tried with the following code:我尝试过使用以下代码:

test_df = pd.DataFrame([['P', 10, np.nan, 5, 9], ['Q', 19, np.nan, np.nan, 4], ['R', 8, np.nan, 3, 7],
                        ['S', 20, np.nan, 3, 7], ['T', 4, np.nan, 2, np.nan]], columns=list('ABCDE'))
priority_list = [list('ABDE'), list('ABD'), list('ABE')]
for elem in priority_list:
    test_df = test_df.loc[test_df[elem].notna()]
    print(test_df)

But this is throwing the following error:但这会引发以下错误:

File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 879, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Python37\lib\site-packages\pandas\core\indexing.py", line 1097, in _getitem_axis
    raise ValueError("Cannot index with multidimensional key")
ValueError: Cannot index with multidimensional key

How to overcome this issue and check for multiple columns for non-na values in the dataframe?如何克服此问题并检查 dataframe 中的non-na值的多列?

Use DataFrame.all for test if all selected values are True s:使用DataFrame.all进行测试,如果所有选定的值都是True s:

priority_list = [list('ABDE'), list('ABD'), list('ABE')]
for elem in priority_list:
    test_df = test_df.loc[test_df[elem].notna().all(axis=1)]
    print(test_df)
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM