網站首頁 編程語言 正文
一、字符串離散化示例
對于一組電影數據,我們希望統計電影分類情況,應該如何處理數據?(每一個電影都有很多個分類)
思路:首先構造一個全為0的數組,列名為分類,如果某一條數據中分類出現過,就讓0變為1
代碼:
# 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)) #統計分類的列表 temp_list = df["Genre"].str.split(",").tolist() #[[],[],[]] genre_list = list(set([i for j in temp_list for i in j])) #構造全為0的數組 zeros_df = pd.DataFrame(np.zeros((df.shape[0],len(genre_list))),columns=genre_list) # print(zeros_df) #給每個電影出現分類的位置賦值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)) #統計每個分類的電影的數量和 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()
結果:
?二、數據合并
2.1 join
join:默認情況下他是把行索引相同的數據合并到一起
?2.2 merge
merge:按照指定的列把數據按照一定的方式合并到一起
?三、數據的分組和聚合
示例:現在我們有一組關于全球星巴克的店鋪的統計數據,如果我想知道美國的星巴克數量和中國的哪個多,或者我想知道中國每個省份的星巴克的數量情況,應該怎么辦?
代碼:
import pandas as pd file_path = "./starbucks_store_worldwide.csv" df = pd.read_csv(file_path) grouped = df.groupby(by="Country")#按照分組查詢 # print(grouped) #DataFrameGroupBy #可以進行遍歷 # for i,j in grouped: # print(i) # print("-"*100) # print(j,type(j)) # print("*"*100) # 調用聚合方法 country_count = grouped["Brand"].count() # print(country_count["US"]) # print(country_count["CN"]) #統計中國每個省店鋪的數量 china_data = df[df["Country"] =="CN"] grouped = china_data.groupby(by="State/Province").count()["Brand"] # print(grouped) # 數據按照多個條件進行分組,返回Series grouped = df["Brand"].groupby(by=[df["Country"],df["State/Province"]]).count() # print(grouped) # print(type(grouped)) # 數據按照多個條件進行分組,返回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']
重新設置index:df.reindex(list("abcdef"))
指定某一行作為index:df.set_index("Country",drop=False)
返回index的唯一值:df.set_index("Country").index.unique()
總結
原文鏈接:https://blog.csdn.net/weixin_43238102/article/details/122433136
相關推薦
- 2022-06-17 Python實現一維插值方法的示例代碼_python
- 2022-12-06 React-Hook中使用useEffect清除定時器的實現方法_React
- 2022-05-11 批量導入模板數據的時候遇到的一些關于多線程的問題
- 2022-06-18 C語言?詳解字符串基礎_C 語言
- 2022-05-01 python?pandas庫讀取excel/csv中指定行或列數據_python
- 2022-08-15 初步了解MyBatis的相關配置
- 2023-10-09 element-ui,tree樹形控件,通過接口返回數據判斷是否繼續拿子節點
- 2022-10-06 SQL語句中的ON?DUPLICATE?KEY?UPDATE使用_MsSql
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支