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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pandas库的学习使用(一)

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pandas库的学习使用(一) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Pandas DataFrame基礎知識
本章主要講述pandas一些Pandas DataFrame的基礎知識
1.1簡介
Pandas [1] 是python的一個數據分析包,最初由AQR Capital Management于2008年4月開發,并于2009年底開源出來,目前由專注于Python數據包開發的PyData開發team繼續開發和維護,屬于PyData項目的一部分。Pandas最初被作為金融數據分析工具而開發出來,因此,pandas為時間序列分析提供了很好的支持。 Pandas的名稱來自于面板數據(panel data)和python數據分析(data analysis)。panel data是經濟學中關于多維數據集的一個術語,在Pandas中也提供了panel的數據類型。

1.2加載數據集

1 import pandas as pd2 # file_path=(r'data/gapminder.tsv')3 # df=pd.read_csv(r'C:\Users\123\Desktop\pandas_exerices\data\gapminder.tsv',sep=',')#輸出的數據其中的符號為,4 df=pd.read_csv(r'C:\Users\123\Desktop\pandas_exerices\data\gapminder.tsv',sep='\t')#輸出的數據多一個table5 print(df.head())#讀取前5行的數據6 print(type(df))#檢查返回的數據是否為DataFrame7 print(df.shape)#Read the shape of the data, namely rows and columns(rows,columns)8 print(df.columns)#讀取列名9 print(df.dtypes)#讀取每列的dtype 10 print(df.info())#獲取更多信息

1.2查看列和行&單元格

1.2.1獲取列子集
1 country_1=df[‘country’]#讀取數據集中名為country的一列的所python基礎教程有內容并且命名為country_1
2 print(country_1.head())#打印前五列
3 print(country_1.tail())#打印后五列
4
5 subset=df[[‘country’, ‘continent’, ‘year’]]#通過列名指定多列
6 print(subset.head())

1.2.2獲取行子集

1 print(df.loc[0])#讀取第一行,python從0開始計數 2 print(df.loc[99])#讀取第1003 # print(df.loc[-1])無法讀取該數據集,因為該數據集會從搜索-14 number_of_rows=df.shape[0] 5 last_row=number_of_rows-1 6 print(df.loc[last_row]) 7 print(df.tail(n=1))#tail和loc輸出的最后一行數據類型不同\

1 #tail和loc輸出的最后一行數據類型不同 2 subset_1=df.loc[0] 3 subset_2=df.tail(1) 4 subset_3=df.iloc[0] 5 print(type(subset_2)) 6 print(type(subset_1)) 7 print(type(subset_3))

1.2.2.2

1.2.3混合
1.2.3.1 獲取列子集

1 #使用loc獲取列子集2 #注意冒號使用位置3 #冒號用于選擇所有行4 #df.iloc[:,[]]獲取列子集5 subset = df.loc[:, ['year', 'pop']]6 print(subset.head())7 #iloc支持使用整數8 #iloc支持-1選擇最后一列9 subset = df.iloc[:,[1,0,-1]] 10 print(subset.head()) 11 #通過范圍選擇列子集 12 print("*"*100) 13 small_range =list(range(0,6)) 14 print(small_range) 15 subset = df.iloc[:,small_range] 16 print(subset.head()) 17 18 large_range =list(range(3,6)) 19 print(large_range) 20 subset =df.iloc[:,large_range] 21 print(subset.head()) 22 # super_range =list(range(100)) 23 # subse_big =df.iloc[:,super_range] 24 # print(subse_big.head()) 25 # IndexError: positional indexers are out-of-bounds 26 print(df.columns) 27 subset = df.iloc[:,:3]#獲取前三列 28 print(subset.head()) 29 subset = df.iloc[:,3:6]#獲取第4 5 6列數據 30 print(subset.head()) 31 subset = df.iloc[:,0:6:2]#2為步長,此處獲取的是135 32 subset = df.iloc[:,0:6:3]#3為步長,此處獲取的是1433 subset = df.iloc[:,::] 34 print(subset.head())

1 #使用loc
2 print(df.loc[42,‘country’])
3 #使用iloc
4 print(df.iloc[42,0])

df.iloc[:,:0:6:1]=df,iloc[:,::]=df.iloc[:,0:6:]=df.iloc[:,0::1]=…

1.2.4獲取行和列的子集

1 #使用loc 2 print(df.loc[42,'country']) 3 #使用iloc 4 print(df.iloc[42,0])

1 #獲取continentpopgdpPercap
2 print(df.iloc[[99,98,64],[1,4,5]])
3 print(df.loc[[99,98,64],[‘continent’,‘pop’,‘gdpPercap’]])
4 #注意,在iloc和loc行部分可以使用切片語法
5 print(df.iloc[10:15,[2,4,5]])
6 print(df.loc[10:15,[‘year’,‘pop’,‘gdpPercap’]])

1.4分組和聚合計算
1.4.1分組方式

1 #分組計算2 print(df.groupby('year')['lifeExp'].mean())3 #詳細解釋4 groupby_year_df=df.groupby('year')5 print(groupby_year_df)6 print(type(groupby_year_df))7 #step28 print("*"*100)9 groupby_year_df_lifeExp=groupby_year_df['lifeExp'] 10 print(groupby_year_df_lifeExp) 11 print(type(groupby_year_df_lifeExp)) 12 13 mean_lifeExp_by_year=groupby_year_df_lifeExp.mean() 14 print(mean_lifeExp_by_year)

#計算每個國家的平均壽命
print(df.groupby(‘country’)[‘lifeExp’].mean().head(10))

#計算國家gdp和平均年齡
print(df.groupby([‘year’,‘continent’])[[‘lifeExp’,‘gdpPercap’]].mean())

#平鋪數據 falt=df.groupby(['year','continent'])[['lifeExp','gdpPercap']].mean().reset_index() print(falt)

1.4.2分組頻率計數
#計算頻率各大洲國家出現的頻率
print(df.groupby(‘continent’)[‘country’].nunique())

1.5基本繪圖
1 global_yearly_life_expectancy=df.groupby(‘year’)[‘lifeExp’].mean()
2 print(global_yearly_life_expectancy)
3 global_yearly_life_expectancy.plot()

Day 2復習

1 import pandas as pd2 df=pd.read_csv(r'C:\Users\123\Desktop\pandas_exerices\data\housing.csv',sep=',')3 print(df.head())#讀取頭文件前54 print(df.columns)#讀取列名5 #計算'Neighborhood'的平均、'Year.Built'6 year_groupby = df.groupby('Neighborhood')['Year.Built'].mean().reset_index()7 print(year_groupby)8 year = df.groupby('Year.Built')['Boro'].nunique()9 print(year) 10 print(df.iloc[:,::]) 11 print('*'*100) 12 print(df.iloc[:,[1,2,3]])#讀取第二三四列 13 print('*'*100) 14 print(df.loc[10:15,['Building.Classification', 'Total.Units', 'Year.Built']]) 15 #讀取第10-15行(row+1'Building.Classification', 'Total.Units', 'Year.Built'16 print(df.tail(5))#讀取倒數5

總結

以上是生活随笔為你收集整理的Pandas库的学习使用(一)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。