开课吧--Python数据分析--第2节 太乙真人的奇珍异宝--互动练习:众里寻她千百度
根据公司的员工信息登记表数据,获取相关信息:
from pandas import Series,DataFrame
index_list = ['001','002','003','004','005','006','007','008','009','010']
name_list = ['李白','王昭君','诸葛亮','狄仁杰','孙尚香','妲己','周瑜','张飞','王昭君','大乔']
age_list = [25,28,27,25,30,29,25,32,28,26]
salary_list = ['10k','12.5k','20k','14k','12k','17k','18k','21k','22k','21.5k']
marital_list = ['NO','NO','YES','YES','NO','NO','NO','YES','NO','YES']
dic={
'姓名': Series(data=name_list,index=index_list),
'年龄': Series(data=age_list,index=index_list),
'薪资': Series(data=salary_list,index=index_list),
'婚姻状况': Series(data=marital_list,index=index_list)
}
df=DataFrame(dic)
# 1. 获取工号为003~007的所有员工信息
df['003':'007']
df.loc['003':'007',:]
df.iloc[2:7]
# 2. 获取所有员工的年龄和工资信息;
df[['年龄','薪资']
df.loc[:,['年龄','薪资']]
df.iloc[:,[1,2]]
# 3. 查看一个你感兴趣员工的婚姻状况。
df.loc['009',['姓名','婚姻状况']]
df.iloc[8,[0,3]]