網(wǎng)站首頁 編程語言 正文
我使用的 Pandas 版本如下,順便也導(dǎo)入 Pandas 庫。
>>> import pandas as pd >>> pd.__version__ '0.25.1'
在開始前先確保解釋器和數(shù)據(jù)集在同一目錄下:
>>> import os >>> os.chdir('D://source/dataset') # 這是我的數(shù)據(jù)集所在目錄 >>> os.listdir() # 確認(rèn)此目錄已經(jīng)存在 IMDB-Movie-Data 數(shù)據(jù)集 ['drinksbycountry.csv', 'IMDB-Movie-Data.csv', 'movietweetings', 'titanic_eda_data.csv', 'titanic_train_data.csv']
準(zhǔn)備工作就位后,正式開始數(shù)據(jù)處理技巧之旅。
1 Pandas 移除某列
導(dǎo)入數(shù)據(jù)
>>> df = pd.read_csv("IMDB-Movie-Data.csv") >>> df.head(1) # 導(dǎo)入并顯示第一行 ? ?Rank ? ? ? ? ? ? ? ? ? ?Title ? ? ? ? ? ? ? ? ? ?Genre ?... ? Votes Revenue (Millions) Metascore 0 ? ? 1 ?Guardians of the Galaxy ?Action,Adventure,Sci-Fi ?... ?757074 ? ? ? ? ? ? 333.13 ? ? ?76.0 [1 rows x 12 columns]
使用 pop 方法移除指定列:
>>> meta = df.pop("Title").to_frame() # 移除 Title 列
確認(rèn)是否已被移除:
>>> df.head(1) # df 變?yōu)?11列 ? ?Rank ? ? ? ? ? ? ? ? ? ?Genre ?... Revenue (Millions) Metascore 0 ? ? 1 ?Action,Adventure,Sci-Fi ?... ? ? ? ? ? ? 333.13 ? ? ?76.0 [1 rows x 11 columns]
2 統(tǒng)計標(biāo)題單詞數(shù)
pop 后得到 meta,顯示 meta 前 3 行:
>>> meta.head(3) Title 0 Guardians of the Galaxy 1 Prometheus 2 Split
標(biāo)題是由單詞組成,中間用空格分隔。
# .str.count(" ") + 1 得到單詞個數(shù) >>> meta["words_count"] = meta["Title"].str.count(" ") + 1 >>> meta.head(3) # words_count 列代表單詞個數(shù) Title words_count 0 Guardians of the Galaxy 4 1 Prometheus 1 2 Split 1
3 Genre 頻次統(tǒng)計
下面統(tǒng)計電影 Genre 的頻次,
>>> vc = df["Genre"].value_counts()
下面顯示電影 Genre 的 Top5 ,最高頻為出現(xiàn) 50 次的 Action,Adventure,Sci-Fi 類,次之為 48 次的 Drama 類:
>>> vc.head() Action,Adventure,Sci-Fi 50 Drama 48 Comedy,Drama,Romance 35 Comedy 32 Drama,Romance 31 Name: Genre, dtype: int64
展示 Top5 的餅狀圖:
>>> import matplotlib.pyplot as plt >>> vc[:5].plot(kind='pie')>>> plt.show()
原文鏈接:https://blog.csdn.net/weixin_38037405/article/details/123869850
相關(guān)推薦
- 2022-06-24 Go單體服務(wù)開發(fā)最佳實踐總結(jié)_Golang
- 2023-07-03 react之自定義hooks
- 2022-04-28 Python字符串的創(chuàng)建和駐留機(jī)制詳解_python
- 2022-05-01 pytorch中的torch.nn.Conv2d()函數(shù)圖文詳解_python
- 2022-05-22 Docker容器之間數(shù)據(jù)傳輸?shù)膶崿F(xiàn)_docker
- 2022-11-03 ios利用RunLoop原理實現(xiàn)去監(jiān)控卡頓實例詳解_IOS
- 2022-12-28 React組件通信實現(xiàn)流程詳解_React
- 2023-02-02 一文教你利用Python制作一個生日提醒_python
- 最近更新
-
- 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錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(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)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支