在python中比较另一个属性比较后的一个属性和打印数据

时间:2018-06-02 16:26:52

标签: python-3.x pandas numpy

如何比较另一个属性比较后的一个属性和python中的打印数据?

#Python code:

import numpy as np
import pandas as pd

#getting data from csv
#np_disease has two values 1->yes,2->no
#np_plastic_used has two values 1->yes , 2->no

df = pd.read_csv('PUBLIC.csv')
data=np.array(df[['slno','disease','plastic_used']])
np_disease=np.array(df[['disease']])
np_plastic_used=np.array(df[['plastic_used']])
np_plastic_usage=plastic_used[plastic_used==1]
np_patients=disease[disease==1]

patient_who_used_plastic= ''#what operation I will do here?#

print("No:of ppl got disease = " + str(len(np_patients)))
print("No:of ppl used plastic = " + str(len(np_plastic_usage)))
print("No:of ppl become patients because of platic usage= " + str(patient_who_used_plastic))

我也想打印

print(data_of_ppl_whose_disease_is_one_and_plastic_used_is_one)
#with sl.no

输入:

data= is [[100 1 1][101 2 2][102 1 2]......[1251 1 1]] upto 1251 rows

输出

No:of ppl got disease = 250 
No:of ppl used plastic = 350
No:of ppl become patients because of platic usage = 150

data_of_ppl_whose_disease_is_one_and_plastic_used_is_one如下:

outputdata= is [[100 1 1][107 1 1]......[1251 1 1]] upto 150 rows

1 个答案:

答案 0 :(得分:1)

您可以通过将列与m1进行比较来创建布尔值掩码m21m3&的两个掩码链AND用于sum

然后,对于计数True,这些掩码 - 1m3 s之类的过程。

最后过滤器使用boolean indexing#sample data a = np.array([[100, 1, 1],[101, 2, 2],[102, 1, 2],[103, 1, 2],[1251, 1, 1]]) df = pd.DataFrame(a, columns=['slno','disease','plastic_used']) #with real data use #df = pd.read_csv('PUBLIC.csv') print (df) slno disease plastic_used 0 100 1 1 1 101 2 2 2 102 1 2 3 103 1 2 4 1251 1 1

m1 = (df['plastic_used'] == 1)
m2 = (df['disease'] == 1)
m3 = m1 & m2

np_plastic_usage = m1.sum()
np_patients = m2.sum()
patient_who_used_plastic = m3.sum()

data_of_ppl_whose_disease_is_one_and_plastic_used_is_one = df[m3]

print("No:of ppl got disease = {}".format(np_patients))
print("No:of ppl used plastic = {}".format(np_plastic_usage))
print("No:of ppl become patients because of platic usage = {}".format(patient_who_used_plastic))
No:of ppl got disease = 4
No:of ppl used plastic = 2
No:of ppl become patients because of platic usage = 2

print(data_of_ppl_whose_disease_is_one_and_plastic_used_is_one)
   slno  disease  plastic_used
0   100        1             1
4  1251        1             1
.gitignore