pandas基础1

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pyplot as mpl
import pandas as pd
mpl.rcParams['font.sans-serif'] = ['SimHei']
mpl.rcParams['axes.unicode_minus'] = False
#获取数据框
df = pd.read_csv("C:/Users/23608/Desktop/IMDB-Movie-Data.csv")
df.head()
RankTitleGenreDescriptionDirectorActorsYearRuntime (Minutes)RatingVotesRevenue (Millions)Metascore
01Guardians of the GalaxyAction,Adventure,Sci-FiA group of intergalactic criminals are forced ...James GunnChris Pratt, Vin Diesel, Bradley Cooper, Zoe S...20141218.1757074333.1376.0
12PrometheusAdventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te...Ridley ScottNoomi Rapace, Logan Marshall-Green, Michael Fa...20121247.0485820126.4665.0
23SplitHorror,ThrillerThree girls are kidnapped by a man with a diag...M. Night ShyamalanJames McAvoy, Anya Taylor-Joy, Haley Lu Richar...20161177.3157606138.1262.0
34SingAnimation,Comedy,FamilyIn a city of humanoid animals, a hustling thea...Christophe LourdeletMatthew McConaughey,Reese Witherspoon, Seth Ma...20161087.260545270.3259.0
45Suicide SquadAction,Adventure,FantasyA secret government agency recruits some of th...David AyerWill Smith, Jared Leto, Margot Robbie, Viola D...20161236.2393727325.0240.0
#将评分一栏单独提取出来
cinema = df["Rating"]
cinema
0      8.1
1      7.0
2      7.3
3      7.2
4      6.2
      ... 
995    6.2
996    5.5
997    6.2
998    5.6
999    5.3
Name: Rating, Length: 1000, dtype: float64
#转化为numpy格式
movies = np.array(cinema)
#将数组内的结果求和除以数组的shape得到平均值
result = sum(movies) / movies.shape[0]
result = round(result,2)
result

在这里插入图片描述

tx1 = df["Rating"]
tx2 = df["Runtime (Minutes)"]
print("Rating",tx1.head())
print("Runtime (Minutes)",tx2.head())

在这里插入图片描述

plt.subplots(1, 1,figsize=(10,8), dpi=100)
plt.scatter(y, tx1,label='Rating')
plt.title('Rating分布')
plt.legend()
 
plt.subplots(1, 1,figsize=(10,8), dpi=100)
plt.scatter(y,tx2,label='Runtime (Minutes)')
plt.title('Runtime (Minutes)分布')
plt.legend()
plt.show()

在这里插入图片描述
在这里插入图片描述

tx3 = df["Genre"]
tx4 = tx3.iloc[2].split(",")
temp = []
for i in range(tx3.shape[0]):
    tx4 = np.array(tx3.iloc[i].split(","))
    for j in range(tx4.shape[0]):
        temp.append(tx4[j])
temp
result = np.array(temp)
print(result.shape[0])
result[:5]

在这里插入图片描述

df2 = pd.DataFrame({"key":result, })
df2.merge(df2)

在这里插入图片描述

df3 = df2.groupby(["key"]).size()
df3
x_axis = df3.index
y_axis = df3.values
x_axis

在这里插入图片描述

y_axis

在这里插入图片描述

plt.subplots(1, 1,figsize=(10,8), dpi=100)
plt.pie(y_axis,labels=x_axis)
plt.title('不同类别的电影数量')
plt.legend()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值