網站首頁 編程語言 正文
我使用的 Pandas 版本如下,順便也導入 Pandas 庫。
>>> import pandas as pd >>> pd.__version__ '0.25.1'
在開始前先確保解釋器和數據集在同一目錄下:
>>> import os >>> os.chdir('D://source/dataset') # 這是我的數據集所在目錄 >>> os.listdir() # 確認此目錄已經存在 IMDB-Movie-Data 數據集 ['drinksbycountry.csv', 'IMDB-Movie-Data.csv', 'movietweetings', 'titanic_eda_data.csv', 'titanic_train_data.csv']
準備工作就位后,正式開始數據處理技巧之旅。
1 Pandas 移除某列
導入數據
>>> df = pd.read_csv("IMDB-Movie-Data.csv") >>> df.head(1) # 導入并顯示第一行 ? ?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 列
確認是否已被移除:
>>> df.head(1) # df 變為 11列 ? ?Rank ? ? ? ? ? ? ? ? ? ?Genre ?... Revenue (Millions) Metascore 0 ? ? 1 ?Action,Adventure,Sci-Fi ?... ? ? ? ? ? ? 333.13 ? ? ?76.0 [1 rows x 11 columns]
2 統計標題單詞數
pop 后得到 meta,顯示 meta 前 3 行:
>>> meta.head(3) Title 0 Guardians of the Galaxy 1 Prometheus 2 Split
標題是由單詞組成,中間用空格分隔。
# .str.count(" ") + 1 得到單詞個數 >>> meta["words_count"] = meta["Title"].str.count(" ") + 1 >>> meta.head(3) # words_count 列代表單詞個數 Title words_count 0 Guardians of the Galaxy 4 1 Prometheus 1 2 Split 1
3 Genre 頻次統計
下面統計電影 Genre 的頻次,
>>> vc = df["Genre"].value_counts()
下面顯示電影 Genre 的 Top5 ,最高頻為出現 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
相關推薦
- 2022-08-03 C++編程語言中賦值運算符重載函數(operator=)的使用_C 語言
- 2022-12-21 redis保存AtomicInteger對象踩坑及解決_Redis
- 2022-03-31 React-Router6版本的更新引起的路由用法變化_React
- 2024-07-15 linux系統管理高級命令(練習)(six day)
- 2022-08-19 Python截取字符串的簡單方法實例_python
- 2022-06-14 golang?channel管道使用示例解析_Golang
- 2022-08-03 python基礎之//、/與%的區別詳解_python
- 2022-07-14 Python?socket如何實現服務端和客戶端數據傳輸(TCP)_python
- 最近更新
-
- 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同步修改后的遠程分支