如何在Python 3.6.3中删除包含Nan的行

时间:2018-02-05 16:13:15

标签: pandas dataframe python-3.6 nan

我想用" nan"删除行或" -nan":

Excel

读:

excel_file = 'originale_ridotto.xlsx'

df = pd.read_excel(excel_file, na_values="NaN")
print(df)
print("I am here")
df.dropna(axis=0, how="any")
print(df)

dataframe colunmns的输出(Python 3.6.3):

Data e ora            Potenza Teorica Totale CC [kW]
0  01/01/2017  00:05                               0   
1  01/01/2017  00:10                               0   
2  01/01/2017  00:15                               0   
3  01/01/2017  00:20                               0   
4  01/01/2017  00:25                               0   
5  01/01/2017  00:30                               0   
6  01/01/2017  00:35                               0   
7  01/01/2017  00:40                               0   

Potenza Attiva Totale AC [kW]  Energia totale cumulata al contatore [kWh]  \
0                           0                                   7760812.5   
1                           0                                   7760812.5   
2                           0                                   7760812.5   
3                           0                                   7760812.5   
4                           0                                   7760812.5   
5                           0                                   7760812.5   
6                           0                                   7760812.5   
7                           0                                   7760812.5   

   Temperatura modulo [°C]  Irraggiamento [W/m2]  
0                        0                   5.0   
1                        0                   6.0   
2                        0                   NaN   
3                        0                   2.0   
4                        0                   3.0   
5                        0                   NaN   
6                        0                   7.0   
7                        0                   9.0   

   Potenza Attiva Inv.1Blocco1 [kW]  
0                                 0  
1                                 0  
2                                 0  
3                                 0  
4                                 0  
5                                 0  
6                                 0  
7                                 0  


   Data e ora         Potenza Teorica Totale CC [kW]  
0  01/01/2017  00:05                               0   
1  01/01/2017  00:10                               0   
2  01/01/2017  00:15                               0   
3  01/01/2017  00:20                               0   
4  01/01/2017  00:25                               0   
5  01/01/2017  00:30                               0   
6  01/01/2017  00:35                               0   
7  01/01/2017  00:40                               0   

   Potenza Attiva Totale AC [kW]  Energia totale cumulata al contatore [kWh]  
0                              0                                   7760812.5   
1                              0                                   7760812.5   
2                              0                                   7760812.5   
3                              0                                   7760812.5   
4                              0                                   7760812.5   
5                              0                                   7760812.5   
6                              0                                   7760812.5   
7                              0                                   7760812.5   

   Temperatura modulo [°C]  Irraggiamento [W/m2]  \
0                        0                   5.0   
1                        0                   6.0   
2                        0                   NaN   
3                        0                   2.0   
4                        0                   3.0   
5                        0                   NaN   
6                        0                   7.0   
7                        0                   9.0   

   Potenza Attiva Inv.1Blocco1 [kW]  
0                                 0  
1                                 0  
2                                 0  
3                                 0  
4                                 0  
5                                 0  
6                                 0  
7                                 0 

df.dropna(axis=0, how="any")不会删除这些行。为什么?

你可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您正在创建已清理的数据框,但您并未记得"记住"它。 df.dropna(how='any')返回已清理的df - 您需要分配它,然后使用它:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0,1000,size=(10, 10)), columns=list('ABCDEFGHIJ'))

# ignoring the warnings
df['A'][2] = np.NaN
df['C'][3] = np.NaN
df['I'][5] = np.NaN
df['E'][7] = np.NaN

print(df)

df = df.dropna(how='any')  # this returns a NEW dataframe, it does not modify in place

print(df)

输出:

       A    B      C    D      E    F    G    H      I    J
0  314.0  664  855.0  101  764.0  251  503  783  153.0  474
1  903.0   77  546.0  205  113.0  519  115   45  988.0  964
2    NaN  155  481.0  243  165.0  696  255  123  802.0  228
3  406.0  603    NaN   84  390.0  545  651  549  440.0  982
4  796.0  626  139.0  810  474.0  257  407  264  680.0  164
5  443.0  132  545.0  380  420.0  885  704  596    NaN  778
6  285.0  317  238.0  437  508.0  189  501  738  605.0  290
7  144.0  426  220.0  573    NaN  758  581  420  544.0  173
8  864.0  369  541.0  405  863.0   45  522  178  705.0  419
9  936.0  664  547.0  793   68.0   77  364  633  547.0  790

       A    B      C    D      E    F    G    H      I    J
0  314.0  664  855.0  101  764.0  251  503  783  153.0  474
1  903.0   77  546.0  205  113.0  519  115   45  988.0  964
4  796.0  626  139.0  810  474.0  257  407  264  680.0  164
6  285.0  317  238.0  437  508.0  189  501  738  605.0  290
8  864.0  369  541.0  405  863.0   45  522  178  705.0  419
9  936.0  664  547.0  793   68.0   77  364  633  547.0  790