一般情况下我们从一堆数据中选择我们获取想要的数据会通过一下方式:
(1)创建链表或数组;
(2)用for 循环遍历所有数据,将想要的存入链表或数组。
但是python中我们不需要这么做,我们可以用Pandas库帮我们解决这个问题:具体使用看实例:
import numpy as np
import pandas as pd
from time import time
from IPython.display import display # 允许为DataFrame使用display()
import visuals as vs
data = pd.read_csv("census.csv")
n_records =len(data['income'])
n=0
m=0
n_greater_50k = data[data['income'] == '>50K'].shape[0]#替换for循环
n_at_most_50k = data[data['income'] == '<=50K'].shape[0]
print "Total number of records: {}".format(n_records)
print "Individuals making more than $50,000: {}".format(n_greater_50k)
print "Individuals making at most $50,000: {}".format(n_at_most_50k)
print "Percentage of individuals making more than $50,000: {:.2f}%".format(greater_percent)