pandas打印全部列_python——pandas练习题1-5
生活随笔
收集整理的這篇文章主要介紹了
pandas打印全部列_python——pandas练习题1-5
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
練習1-開始了解你的數據
探索Chipotle快餐數據
相應數據集:chipotle.tsv
import pandas as pd chipo=pd.read_csv("exercise_data/chipotle.tsv",sep='t') chipo.head(5)chipo.shape[0] #查看有多少行4622chipo.shape[1] #查看有多少列5chipo.columns #打印所有列名Index(['order_id', 'quantity', 'item_name', 'choice_description','item_price'],dtype='object')chipo.index #打印索引RangeIndex(start=0, stop=4622, step=1)c=chipo[['item_name','quantity']].groupby(['item_name'],as_index=False).agg({'quantity':sum}) c.sort_values(['quantity'],ascending=False,inplace=True) c.head(5) #被下單數最多商品(item)是什么?chipo['item_name'].nunique() #在item_name這一列中,一共有多少種商品被下單?5chipo['choice_description'].value_counts().head() #在choice_description中,下單次數最多的商品是什么?chipo['quantity'].sum() #一共有多少商品被下單?4972lambda函數,apply和map區別
dollarizer = lambda x: float(x[1:-1]) #x為item_price這列組成的數組,對其切片,取第二個(下標為1)到最后一個數,即把美元符號去掉 chipo['item_price'] = chipo['item_price'].apply(dollarizer) #將item_price轉換為浮點數chipo['revenue']=chipo['quantity']*chipo['item_price'] total=round(chipo['revenue'].sum(),2) total #在該數據集對應的時期內,收入(revenue)是多少39237.02chipo['order_id'].nunique() #在該數據集對應的時期內,一共有多少訂單?1834先分組,再求和,再求平均
c=round(chipo[['order_id','revenue']].groupby(['order_id']).agg({'revenue':sum})['revenue'].mean(),2) c #每一單(order)對應的平均總價是多少?21.39練習2-數據過濾與排序
探索2012歐洲杯數據
相應數據集:Euro2012_stats.csv
import pandas as pd euro=pd.read_csv("Euro2012_stats.csv") euro.head()#只選取 Goals 這一列 euro['Goals']0 4 1 4 2 4 3 5 4 3 5 10 6 5 7 6 8 2 9 2 10 6 11 1 12 5 13 12 14 5 15 2 Name: Goals, dtype: int64#有多少球隊參與了2012歐洲杯? euro.shape[0]16#該數據集中一共有多少列(columns)? euro.shape[1]35#將數據集中的列Team, Yellow Cards和Red Cards單獨存為一個名叫discipline的數據框 discipline=euro[['Team','Yellow Cards','Red Cards']] discipline.head() #對數據框discipline按照先Red Cards再Yellow Cards進行排序 discipline.sort_values(['Red Cards','Yellow Cards'],ascending=False)#計算每個球隊拿到的黃牌數的平均值 round(discipline['Yellow Cards'].mean())7#找到進球數Goals超過6的球隊數據 euro[euro['Goals']>6]#選取以字母G開頭的球隊數據 euro[euro['Team'].str.startswith('G')]#選取前7列 euro.iloc[:,0:7] #切片按行號列號切片
#選取除了最后3列之外的全部列 euro.iloc[:,0:-3]按列名切片
#找到英格蘭(England)、意大利(Italy)和俄羅斯(Russia)的射正率(Shooting Accuracy) euro.loc[euro.Team.isin(['England','Italy','Russia']),['Team','Shooting Accuracy']]練習3-數據分組
探索酒類消費數據
相應數據集:drinks.csv
import pandas as pd drinks=pd.read_csv(r"/Users/a2016/exercise_data/drinks.csv") drinks.head()#哪個大陸(continent)平均消耗的啤酒(beer)更多? drinks.groupby(['continent'])['beer_servings'].mean()#打印出每個大陸(continent)的紅酒消耗(wine_servings)的描述性統計值 drinks.groupby(['continent'])['wine_servings'].describe()#打印出每個大陸每種酒類別的消耗平均值 drinks.groupby(['continent']).mean()求中位數 median()
#打印出每個大陸每種酒類別的消耗中位數 drinks.groupby(['continent']).median()#打印出每個大陸對spirit飲品消耗的平均值,最大值和最小值 drinks.groupby(['continent'])['spirit_servings'].agg(['mean','max','min'])練習4-Apply函數
探索1960-2014美國犯罪數據
相應數據集:US_Crime_Rates_1960_2014.csv
import pandas as pd US=pd.read_csv(r"/Users/a2016/exercise_data/US_Crime_Rates_1960_2014.csv") US.head()#每一列(column)的數據類型是什么樣的? US.dtypesYear int64 Population int64 Total int64 Violent int64 Property int64 Murder int64 Forcible_Rape int64 Robbery int64 Aggravated_assault int64 Burglary int64 Larceny_Theft int64 Vehicle_Theft int64 dtype: object注意到Year的數據類型為int64,但是pandas有一個不同的數據類型去處理時間序列(time series),將year的數據類型更改。
#將Year的數據類型轉換為 datetime64 US['Year']=pd.to_datetime(US['Year'],format='%Y') US.dtypes Year datetime64[ns] Population int64 Total int64 Violent int64 Property int64 Murder int64 Forcible_Rape int64 Robbery int64 Aggravated_assault int64 Burglary int64 Larceny_Theft int64 Vehicle_Theft int64 dtype: object#將列Year設置為數據框的索引 US = US.set_index('Year', drop = True) US.head()#刪除名為Total的列 del US['Total'] US.head()#日期匯總數據,將數據以10AS十年聚合日期第一天開始的形式進行聚合 索引 US.resample('10AS').sum()#按照Year對數據框進行分組并求和***注意Population這一列,若直接對其求和,是不正確的***
resample 的介紹
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html?pandas.pydata.org# 用resample去得到“Population”列的每十年中的最大值 population = US['Population'].resample('10AS').max() populationYear 1960-01-01 201385000 1970-01-01 220099000 1980-01-01 248239000 1990-01-01 272690813 2000-01-01 307006550 2010-01-01 318857056 Freq: 10AS-JAN, Name: Population, dtype: int64#讓每十年中的最大值代替population這一列 US1['Population']=population US1#何時是美國歷史上生存最危險的年代? US.idxmax(0)練習5-合并
探索虛擬姓名數據
相應數據集:練習自定義的數據集
import pandas as pd import numpy as np# 運行以下代碼 raw_data_1 = {'subject_id': ['1', '2', '3', '4', '5'],'first_name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'], 'last_name': ['Anderson', 'Ackerman', 'Ali', 'Aoni', 'Atiches']}raw_data_2 = {'subject_id': ['4', '5', '6', '7', '8'],'first_name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'], 'last_name': ['Bonder', 'Black', 'Balwner', 'Brice', 'Btisan']}raw_data_3 = {'subject_id': ['1', '2', '3', '4', '5', '7', '8', '9', '10', '11'],'test_id': [51, 15, 15, 61, 16, 14, 15, 1, 61, 16]}# 運行以下代碼 data1 = pd.DataFrame(raw_data_1, columns = ['subject_id', 'first_name', 'last_name']) data2 = pd.DataFrame(raw_data_2, columns = ['subject_id', 'first_name', 'last_name']) data3 = pd.DataFrame(raw_data_3, columns = ['subject_id','test_id'])縱向合并兩個表,用concat。前提是兩個表的列名對應
#將data1和data2兩個數據框縱向合并,命名為all_data all_data=pd.concat([data1,data2]) all_data#將data1和data2兩個數據框橫向合并,命名為all_data_col all_data_col=pd.concat([data1,data2],axis=1) all_data_col#按照subject_id的值對all_data和data3作合并 pd.merge(all_data,data3,on='subject_id')【python】詳解pandas庫的pd.merge函數_brucewong0516的博客-CSDN博客_pd.merge?blog.csdn.net#對data1和data2按照subject_id作連接 pd.merge(data1,data2,on='subject_id',how='inner')#找到 data1 和 data2 合并之后的所有匹配結果 pd.merge(data1,data2,on='subject_id',how='outer')總結
以上是生活随笔為你收集整理的pandas打印全部列_python——pandas练习题1-5的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019蓝桥杯Java决赛题答案_201
- 下一篇: 联想拯救者Y9000P 2022上新:冰