日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

pandas打印全部列_python——pandas练习题1-5

發(fā)布時(shí)間:2023/12/10 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pandas打印全部列_python——pandas练习题1-5 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

練習(xí)1-開始了解你的數(shù)據(jù)

探索Chipotle快餐數(shù)據(jù)

相應(yīng)數(shù)據(jù)集: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) #被下單數(shù)最多商品(item)是什么?

chipo['item_name'].nunique() #在item_name這一列中,一共有多少種商品被下單?5chipo['choice_description'].value_counts().head() #在choice_description中,下單次數(shù)最多的商品是什么?

chipo['quantity'].sum() #一共有多少商品被下單?4972

lambda函數(shù),apply和map區(qū)別

dollarizer = lambda x: float(x[1:-1]) #x為item_price這列組成的數(shù)組,對(duì)其切片,取第二個(gè)(下標(biāo)為1)到最后一個(gè)數(shù),即把美元符號(hào)去掉 chipo['item_price'] = chipo['item_price'].apply(dollarizer) #將item_price轉(zhuǎn)換為浮點(diǎn)數(shù)

chipo['revenue']=chipo['quantity']*chipo['item_price'] total=round(chipo['revenue'].sum(),2) total #在該數(shù)據(jù)集對(duì)應(yīng)的時(shí)期內(nèi),收入(revenue)是多少39237.02chipo['order_id'].nunique() #在該數(shù)據(jù)集對(duì)應(yīng)的時(shí)期內(nèi),一共有多少訂單?1834

先分組,再求和,再求平均

c=round(chipo[['order_id','revenue']].groupby(['order_id']).agg({'revenue':sum})['revenue'].mean(),2) c #每一單(order)對(duì)應(yīng)的平均總價(jià)是多少?21.39

練習(xí)2-數(shù)據(jù)過(guò)濾與排序

探索2012歐洲杯數(shù)據(jù)

相應(yīng)數(shù)據(jù)集: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#有多少球隊(duì)參與了2012歐洲杯? euro.shape[0]16#該數(shù)據(jù)集中一共有多少列(columns)? euro.shape[1]35#將數(shù)據(jù)集中的列Team, Yellow Cards和Red Cards單獨(dú)存為一個(gè)名叫discipline的數(shù)據(jù)框 discipline=euro[['Team','Yellow Cards','Red Cards']] discipline.head()

#對(duì)數(shù)據(jù)框discipline按照先Red Cards再Yellow Cards進(jìn)行排序 discipline.sort_values(['Red Cards','Yellow Cards'],ascending=False)

#計(jì)算每個(gè)球隊(duì)拿到的黃牌數(shù)的平均值 round(discipline['Yellow Cards'].mean())7#找到進(jìn)球數(shù)Goals超過(guò)6的球隊(duì)數(shù)據(jù) euro[euro['Goals']>6]

#選取以字母G開頭的球隊(duì)數(shù)據(jù) euro[euro['Team'].str.startswith('G')]

#選取前7列 euro.iloc[:,0:7] #切片

按行號(hào)列號(hào)切片

#選取除了最后3列之外的全部列 euro.iloc[:,0:-3]

按列名切片

#找到英格蘭(England)、意大利(Italy)和俄羅斯(Russia)的射正率(Shooting Accuracy) euro.loc[euro.Team.isin(['England','Italy','Russia']),['Team','Shooting Accuracy']]

練習(xí)3-數(shù)據(jù)分組

探索酒類消費(fèi)數(shù)據(jù)

相應(yīng)數(shù)據(jù)集:drinks.csv

import pandas as pd drinks=pd.read_csv(r"/Users/a2016/exercise_data/drinks.csv") drinks.head()

#哪個(gè)大陸(continent)平均消耗的啤酒(beer)更多? drinks.groupby(['continent'])['beer_servings'].mean()

#打印出每個(gè)大陸(continent)的紅酒消耗(wine_servings)的描述性統(tǒng)計(jì)值 drinks.groupby(['continent'])['wine_servings'].describe()

#打印出每個(gè)大陸每種酒類別的消耗平均值 drinks.groupby(['continent']).mean()

求中位數(shù) median()

#打印出每個(gè)大陸每種酒類別的消耗中位數(shù) drinks.groupby(['continent']).median()

#打印出每個(gè)大陸對(duì)spirit飲品消耗的平均值,最大值和最小值 drinks.groupby(['continent'])['spirit_servings'].agg(['mean','max','min'])

練習(xí)4-Apply函數(shù)

探索1960-2014美國(guó)犯罪數(shù)據(jù)

相應(yīng)數(shù)據(jù)集: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)的數(shù)據(jù)類型是什么樣的? 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

注意到Y(jié)ear的數(shù)據(jù)類型為int64,但是pandas有一個(gè)不同的數(shù)據(jù)類型去處理時(shí)間序列(time series),將year的數(shù)據(jù)類型更改。

#將Year的數(shù)據(jù)類型轉(zhuǎn)換為 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設(shè)置為數(shù)據(jù)框的索引 US = US.set_index('Year', drop = True) US.head()

#刪除名為Total的列 del US['Total'] US.head()

#日期匯總數(shù)據(jù),將數(shù)據(jù)以10AS十年聚合日期第一天開始的形式進(jìn)行聚合 索引 US.resample('10AS').sum()

#按照Year對(duì)數(shù)據(jù)框進(jìn)行分組并求和

***注意Population這一列,若直接對(duì)其求和,是不正確的***

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

#何時(shí)是美國(guó)歷史上生存最危險(xiǎn)的年代? US.idxmax(0)

練習(xí)5-合并

探索虛擬姓名數(shù)據(jù)

相應(yīng)數(shù)據(jù)集:練習(xí)自定義的數(shù)據(jù)集

import pandas as pd import numpy as np# 運(yùn)行以下代碼 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]}# 運(yùn)行以下代碼 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'])

縱向合并兩個(gè)表,用concat。前提是兩個(gè)表的列名對(duì)應(yīng)

#將data1和data2兩個(gè)數(shù)據(jù)框縱向合并,命名為all_data all_data=pd.concat([data1,data2]) all_data

#將data1和data2兩個(gè)數(shù)據(jù)框橫向合并,命名為all_data_col all_data_col=pd.concat([data1,data2],axis=1) all_data_col

#按照subject_id的值對(duì)all_data和data3作合并 pd.merge(all_data,data3,on='subject_id')

【python】詳解pandas庫(kù)的pd.merge函數(shù)_brucewong0516的博客-CSDN博客_pd.merge?blog.csdn.net#對(duì)data1和data2按照subject_id作連接 pd.merge(data1,data2,on='subject_id',how='inner')

#找到 data1 和 data2 合并之后的所有匹配結(jié)果 pd.merge(data1,data2,on='subject_id',how='outer')

總結(jié)

以上是生活随笔為你收集整理的pandas打印全部列_python——pandas练习题1-5的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。