熊猫:摆脱没有价值的行

时间:2019-05-13 13:37:43

标签: python pandas

我的数据框如下:

time    Time1_high  Price1_high Time2_high  Price2_high Time1_low   Price1_low  Time2_low   Price2_low                      

5   2019-04-22 00:02:00                             
6   2019-04-22 00:02:15                             
7   2019-04-22 00:02:30                         2019-04-22 00:02:30 0.00185691
8   2019-04-22 00:02:45                             
9   2019-04-22 00:03:00                             
10  2019-04-22 00:03:15                             
11  2019-04-22 00:03:30                             
12  2019-04-22 00:03:45                             
13  2019-04-22 00:04:00                 2019-04-22 00:04:15 0.00185499      
14  2019-04-22 00:04:15                             
15  2019-04-22 00:04:30                             
16  2019-04-22 00:04:45                             
17  2019-04-22 00:05:00                             
18  2019-04-22 00:05:15                             
19  2019-04-22 00:05:30                             
20  2019-04-22 00:05:45                             
21  2019-04-22 00:06:00                             
22  2019-04-22 00:06:15         2019-04-22 00:06:15 0.00185391              
23  2019-04-22 00:06:30                             
24  2019-04-22 00:06:45                             
25  2019-04-22 00:07:00                             
26  2019-04-22 00:07:15                             
27  2019-04-22 00:07:30                             
28  2019-04-22 00:07:45 2019-04-22 00:08:00 0.00185587                      
29  2019-04-22 00:08:00         

实际上,我真正需要的是摆脱[Time1_high, Price1_high, Time2_high, Price2_high, Time1_low, Price1_low, Time2_low, Price2_low ]

下所有列都没有数据的列

因此,我实际上只对有价值的列进行了汇总。看起来应该像这样:

    time    Time1_high  Price1_high Time2_high  Price2_high Time1_low   Price1_low  Time2_low   Price2_low                      



    7   2019-04-22 00:02:30                         2019-04-22 00:02:30 0.00185691
    13  2019-04-22 00:04:00                 2019-04-22 00:04:15 0.00185499      
    22  2019-04-22 00:06:15         2019-04-22 00:06:15 0.00185391              
    28  2019-04-22 00:07:45 2019-04-22 00:08:00 0.00185587                      

2 个答案:

答案 0 :(得分:2)

您尝试使用dropna方法吗?

df.dropna(how='all'),如果所有值均为'all',则NA将会消失。

答案 1 :(得分:1)

使用boolean indexing

如果缺失值是空字符串,则不使用DataFrame.iloc过滤所有列,而不必先用DataFrame.ne进行过滤,测试是否等于DataFrame.anyDataFrame.notna,以每行至少返回True

df1 = df[df.iloc[:, 1:].ne('').any(axis=1)]

或者如果缺少的值是NaN,请使用{{3}}:

df1 = df[df.iloc[:, 1:].notna().any(axis=1)]