日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python數據處理的三個實用技巧分享_python

作者:Python學習與數據挖掘 ? 更新時間: 2022-06-04 編程語言

我使用的 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

欄目分類
最近更新