删除带有零的行

时间:2019-07-09 18:55:32

标签: python python-3.x pandas csv

我得到了一个具有不同统计值的csv文件。 NA值已替换为零。如何使用python脚本删除只有零的行。

all_df.dropna(subset=df_all.columns.tolist()[1:], how='all', inplace=True) 
df = df[(df.iloc[:, 1:] != 0).all(1)] 
all_df['activity'] = all_df['activity'].apply(lambda x: re.sub(r'^([a-zA-Z]+).*', r'\1', x)) 
all_df.to_csv('all_data.csv', index=False)

以下是CSV文件的示例:

activity,kurtosisLenIn,kurtosisTimeIn,madLenIn,madTimeIn,maxLenIn,maxTimeIn,meanLenIn
Fbstt,-2.015559753,10.83952356,546.231405,0.044999868,1500,0.275991,774.4545455
Fbstt,18.22973615,60.34424947,120.5485027,0.017036675,1500,0.493632,1436.301075
Fbstt,0.0,0.0,0.0,0.0,0.0,0.0,0.0
Fbstt,0.0,0.0,0.0,0.0,0.0,0.0,0.0
Fbstt,-0.336967713,7.433987655,107.5,0.038565472,417,0.204289,185.3333333
Fbstt,0,0.0,0.0,0,0.0,0.0,0.0
Fbstt,0,0,175.1111111,0.005120444,545,0.012801,399.6666667
Fbstt,0,0,0,0,0,0,0
Fbstt,-1.895337427,-0.356310433,443.1851852,0.06895037,1332,0.202241,692.3333333
Fbstt,15.84040927,179.5524848,129.7876543,0.006645361,1500,0.586817,1431.288889

1 个答案:

答案 0 :(得分:0)

使用pandas,请尝试以下代码:

import pandas as pd

df = pd.read_csv('source.csv')
df = df[(df.iloc[:, 1:] != 0).all(1)]
df.to_csv('output.csv', index=False)