我有一个Pandas数据框架,它有两千多行和近一千列。问题是每行都有不同数量的列,这些列实际上有值。如果行中的值使用的列少于最长行,则其余值为NaN。
它看起来像这样:
0 1 2 3 4 5 6 7
A Hip 22 Hop Hat 3 NaN NaN NaN
B Gig 45 Get Gik 4 Goo 5 NaN
C Lip 34 Lop 2 NaN NaN NaN NaN
D Fip 56 Fup Fik Fap Fat Fot 6.0
字符串
我正在尝试删除(并将其存储为单独的数据框架)每行不包含NaN值的最后两列。所以我想要的是这样的:
0 1 2 3 4 5 6 7
A Hip 22 Hop NaN NaN NaN NaN NaN
B Gig 45 Get Gik 4 NaN NaN NaN
C Lip 34 NaN NaN NaN NaN NaN NaN
D Fip 56 Fup Fik Fap Fat NaN NaN
型
我试图将数据框架转换为嵌套列表,在其中我可以简单地删除NaN值并删除每个列表的最后一列,但它崩溃了。
有什么想法吗
2条答案
按热度按时间syqv5f0l1#
我会尝试这样的东西:
字符串
06odsfpq2#
在反向列上使用
notna
和cumsum
计算掩码:字符串
输出量:
型
中间掩模
m
:型