python数据分析实践(三)
来源:互联网 发布:酷比魔方windows系统 编辑:程序博客网 时间:2024/06/11 23:47
All Time Olympic Games Medals
处理维基百科 All Time Olympic Games Medals 数据集。
import pandas as pd# 读取数据,选取第一列作为index,并跳过第一行,以第二行作为我的column namedf = pd.read_csv('olympics.csv', index_col=0, skiprows=1)# 对column进行部分重命名for col in df.columns: if col[:2]=='01': df.rename(columns={col:'Gold'+col[4:]}, inplace=True) if col[:2]=='02': df.rename(columns={col:'Silver'+col[4:]}, inplace=True) if col[:2]=='03': df.rename(columns={col:'Bronze'+col[4:]}, inplace=True) if col[:1]=='№': df.rename(columns={col:'#'+col[1:]}, inplace=True)names_ids = df.index.str.split('\s\(') # split the index by '('df.index = names_ids.str[0] # the [0] element is the country name (new index) df['ID'] = names_ids.str[1].str[:3] # the [1] element is the abbreviation or ID (take first 3 characters from that)# 删除Total列df = df.drop('Totals')df.head()
任务一:返回DataFrame的第一行
def answer_zero(): return df.iloc[0]
必须使用 iloc , iloc 使用index进行索引, loc 使用index name进行索引。
任务二:返回夏季奥运会获得金牌数最多的国家名
def answer_one(): return df.Gold.idxmax()
一开始考虑使用布尔量索引,df[df.Gold==df.Gold.max()].index[0] ,非常繁琐,结果我去查Series文档发现有非常直接简单的方法。采用idxmax(), idxmin()返回Series最大最小值的索引值。
任务三:返回夏季金牌数和冬季金牌数之差相对于总金牌数最大的国家名(只考虑在夏季冬季奥运会至少都获得过1枚金牌的国家)
def answer_three(): df1 = df[(df.Gold > 0) & (df['Gold.1'] > 0) ] return abs((df1['Gold.1']-df1.Gold)/df1['Gold.2']).idxmax()
任务四:返回一个名为”Points”的Series
def answer_four(): return pd.Series(data = df['Gold.2']*3 + df['Silver.2']*2 + df['Bronze.2']*1,name='Points')
Population Data for Counties and States in the US from 2010 to 2015
处理来自 United States Census Bureau 的人口普查数据集。
import pandas as pdcensus_df = pd.read_csv('census.csv')
任务一:返回拥有最多Country的State
def answer_five(): return census_df[census_df.STNAME != census_df.CTYNAME].STNAME.value_counts().idxmax()
任务二:返回在每个State仅考虑人数最多的三个Country时的三个人数最多的State(2010)
def answer_six(): census_df1 = census_df[census_df.STNAME != census_df.CTYNAME][['STATE','STNAME','CTYNAME','CENSUS2010POP']].set_index('STATE') new_df = pd.DataFrame(columns=['STATE','STNAME','CTYNAME','CENSUS2010POP']) for i in range(56): try: new_df = pd.concat([new_df, census_df1.loc[i+1].sort_values(by='CENSUS2010POP',ascending= False).iloc[:3].reset_index()]) except KeyError: pass return list(new_df.groupby(['STNAME']).sum().CENSUS2010POP.sort_values(ascending= False).iloc[:3].index)
任务三:返回10-15年人数变化最大的Country
def answer_seven(): census_df1 = census_df[census_df.STNAME != census_df.CTYNAME][['CTYNAME','POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013','POPESTIMATE2014','POPESTIMATE2015']].set_index('CTYNAME') return (census_df1.max(axis=1)-census_df1.min(axis=1)).idxmax()
任务四:返回属于Region1或Region2,名字始于’Washington’且15年人数多于14年人数的Country
def answer_eight(): return census_df[(census_df.REGION < 3) & (census_df.POPESTIMATE2015 > census_df.POPESTIMATE2014) & (census_df.CTYNAME.str[:10]=='Washington')][['STNAME','CTYNAME']]
阅读全文
0 0
- python数据分析实践(三)
- python数据分析实践(一)
- python数据分析实践(二)
- python数据分析实践(四)
- 利用Python进行数据分析(三)
- 拉格朗日插值法《python数据分析与挖掘实践》
- python实践项目(三)
- 利用Python数据分析:Numpy基础(三)
- 利用Python数据分析:pandas入门(三)
- 《利用python进行数据分析》学习笔记(三)
- 用Python做数据分析初探(三)
- Python数据分析基础(三)——pandas
- 【Python数据分析与展示】(三)matplotlib库pyplot
- 数据分析(三)
- 数据分析(三)
- Python数据分析学习笔记三
- 分析Python处理基本数据<三>
- python数据分析学习笔记三
- 7.jdbc
- sed,awk笔记
- LCA
- java接口
- 谷歌访问助手(chrome版)安装方法
- python数据分析实践(三)
- 71. Simplify Path
- oneplus5刷机
- 关于Hibernate 在Junit测试中可以成功但是在Web下却无法运行的处理方法
- 《小白兔到大黑牛》第十六篇Scala学习总结
- C++ Builder快捷键
- 类人的普适的计算机视觉是如何感知这个世界的?
- js获取浏览器高度和宽度值
- 数组指针的三种定义形式