pandas :在python中删除编码为0的行 [英] Pandas: Delete rows with different encoding of 0s in python
本文介绍了 pandas :在python中删除编码为0的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经计算了统计值,并将其写入了csv文件。 nan值将替换为零。有只有零的行,有只有0和0.0值的行。如何删除这些行?根据所附的图像行编号5,6(仅0.0s),9和11(均为0s和0.0s)需要删除。
I have calculated statistical values and written them to a csv file. The nan values are replaced with zeros. There are rows with only zeros and there are rows with both 0 and 0.0 values only. How can I delete these rows? According to the attached image rows number 5 , 6 (only 0.0s), 9 and 11 (both 0s and 0.0s) needs to get deleted.
import pandas as pd
all_df = pd.read_csv('source.csv')
all_df.dropna(subset=df_all.columns.tolist()[1:], how='all', inplace=True)
all_df.fillna(0, inplace=True)
all_df.to_csv('outfile.csv', index=False)
推荐答案
使用 all_df [(all_df.T!= 0).any()]
或 all_df [(all_df!= 0).any(axis = 1)]
:
Use all_df[(all_df.T != 0).any()]
or all_df[(all_df != 0).any(axis=1)]
:
all_df = pd.DataFrame({'a':[0,0,0,1], 'b':[0,0,0,1]})
print all_df
a b
0 0 0
1 0 0
2 0 0
3 1 1
all_df = all_df[(all_df.T != 0).any()]
all_df
a b
3 1 1
编辑1 :之后查看数据,一种解决方案是将所有数字列转换为浮点,然后执行操作。此问题来自将初始数据保存到.csv文件的方式。
EDIT 1: After looking at your data, a solution is to convert all numerical columns to float and then do the operations. This problem arises from the way the initial data were saved into the .csv file.
all_df = pd.read_csv('/Users/me/Downloads/Test11.csv')
# do not select 'activity' column
df = all_df.loc[:, all_df.columns != 'activity']
# convert to float
df = df.astype(float)
# remove columns with all 0s
mask = (df != 0).any(axis=1)
df = df[mask]
#mask activity column
recover_lines_of_activity_column = all_df['activity'][mask]
# Final result
final_df = pd.concat([recover_lines_of_activity_column, df], axis = 1)
输出:
这篇关于 pandas :在python中删除编码为0的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文