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

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python數(shù)據(jù)處理的三個實用技巧分享_python

作者:Python學(xué)習(xí)與數(shù)據(jù)挖掘 ? 更新時間: 2022-06-04 編程語言

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

欄目分類
最近更新