網(wǎng)站首頁 編程語言 正文
一、字符串離散化示例
對于一組電影數(shù)據(jù),我們希望統(tǒng)計(jì)電影分類情況,應(yīng)該如何處理數(shù)據(jù)?(每一個(gè)電影都有很多個(gè)分類)
思路:首先構(gòu)造一個(gè)全為0的數(shù)組,列名為分類,如果某一條數(shù)據(jù)中分類出現(xiàn)過,就讓0變?yōu)?
代碼:
# coding=utf-8 import pandas as pd from matplotlib import pyplot as plt import numpy as np file_path = "./IMDB-Movie-Data.csv" df = pd.read_csv(file_path) print(df["Genre"].head(3)) #統(tǒng)計(jì)分類的列表 temp_list = df["Genre"].str.split(",").tolist() #[[],[],[]] genre_list = list(set([i for j in temp_list for i in j])) #構(gòu)造全為0的數(shù)組 zeros_df = pd.DataFrame(np.zeros((df.shape[0],len(genre_list))),columns=genre_list) # print(zeros_df) #給每個(gè)電影出現(xiàn)分類的位置賦值1 for i in range(df.shape[0]): #zeros_df.loc[0,["Sci-fi","Mucical"]] = 1 zeros_df.loc[i,temp_list[i]] = 1 # print(zeros_df.head(3)) #統(tǒng)計(jì)每個(gè)分類的電影的數(shù)量和 genre_count = zeros_df.sum(axis=0) print(genre_count) #排序 genre_count = genre_count.sort_values() _x = genre_count.index _y = genre_count.values #畫圖 plt.figure(figsize=(20,8),dpi=80) plt.bar(range(len(_x)),_y,width=0.4,color="blue") plt.xticks(range(len(_x)),_x) plt.show()
結(jié)果:
?二、數(shù)據(jù)合并
2.1 join
join:默認(rèn)情況下他是把行索引相同的數(shù)據(jù)合并到一起
?2.2 merge
merge:按照指定的列把數(shù)據(jù)按照一定的方式合并到一起
?三、數(shù)據(jù)的分組和聚合
示例:現(xiàn)在我們有一組關(guān)于全球星巴克的店鋪的統(tǒng)計(jì)數(shù)據(jù),如果我想知道美國的星巴克數(shù)量和中國的哪個(gè)多,或者我想知道中國每個(gè)省份的星巴克的數(shù)量情況,應(yīng)該怎么辦?
代碼:
import pandas as pd file_path = "./starbucks_store_worldwide.csv" df = pd.read_csv(file_path) grouped = df.groupby(by="Country")#按照分組查詢 # print(grouped) #DataFrameGroupBy #可以進(jìn)行遍歷 # for i,j in grouped: # print(i) # print("-"*100) # print(j,type(j)) # print("*"*100) # 調(diào)用聚合方法 country_count = grouped["Brand"].count() # print(country_count["US"]) # print(country_count["CN"]) #統(tǒng)計(jì)中國每個(gè)省店鋪的數(shù)量 china_data = df[df["Country"] =="CN"] grouped = china_data.groupby(by="State/Province").count()["Brand"] # print(grouped) # 數(shù)據(jù)按照多個(gè)條件進(jìn)行分組,返回Series grouped = df["Brand"].groupby(by=[df["Country"],df["State/Province"]]).count() # print(grouped) # print(type(grouped)) # 數(shù)據(jù)按照多個(gè)條件進(jìn)行分組,返回DataFrame grouped1 = df[["Brand"]].groupby(by=[df["Country"],df["State/Province"]]).count() grouped2= df.groupby(by=[df["Country"],df["State/Province"]])[["Brand"]].count() grouped3 = df.groupby(by=[df["Country"],df["State/Province"]]).count()[["Brand"]] print(grouped1,type(grouped1)) print("*"*100) print(grouped2,type(grouped2)) print("*"*100) print(grouped3,type(grouped3))
?四、索引
簡單的索引操作:
獲取index:df.index
指定index:df.index=['x','y']
重新設(shè)置index:df.reindex(list("abcdef"))
指定某一行作為index:df.set_index("Country",drop=False)
返回index的唯一值:df.set_index("Country").index.unique()
總結(jié)
原文鏈接:https://blog.csdn.net/weixin_43238102/article/details/122433136
相關(guān)推薦
- 2022-07-18 SQL?Server中字符串函數(shù)的用法詳解_MsSql
- 2022-05-06 C語言隊(duì)列和應(yīng)用詳情_C 語言
- 2022-05-05 Python&Matlab實(shí)現(xiàn)螞蟻群算法求解最短路徑問題的示例_python
- 2022-10-02 C++數(shù)據(jù)結(jié)構(gòu)之紅黑樹的實(shí)現(xiàn)_C 語言
- 2022-11-25 golang?實(shí)現(xiàn)時(shí)間滑動窗口的示例代碼_Golang
- 2022-06-11 嵌入式C語言二級指針在鏈表中的應(yīng)用_C 語言
- 2022-07-06 python如何實(shí)現(xiàn)質(zhì)數(shù)求和_python
- 2022-08-17 R語言UpSet包實(shí)現(xiàn)集合可視化示例詳解_R語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支