Python数据处理的三个实用技巧分享
我使用的 Pandas 版本如下顺便也导入 Pandas 库。123importpandas as pd pd.__version__0.25.1在开始前先确保解释器和数据集在同一目录下1234importos 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 移除某列导入数据123456 dfpd.read_csv(IMDB-Movie-Data.csv) df.head(1)# 导入并显示第一行Rank Title Genre ... Votes Revenue (Millions) Metascore01Guardians of the Galaxy Action,Adventure,Sci-Fi ...757074333.1376.0[1rows x12columns]使用 pop 方法移除指定列1 metadf.pop(Title).to_frame()# 移除 Title 列确认是否已被移除12345 df.head(1)# df 变为 11列Rank Genre ... Revenue (Millions) Metascore01Action,Adventure,Sci-Fi ...333.1376.0[1rows x11columns]2 统计标题单词数pop 后得到 meta显示 meta 前 3 行12345 meta.head(3)Title0Guardians of the Galaxy1Prometheus2Split标题是由单词组成中间用空格分隔。1234567# .str.count( ) 1 得到单词个数 meta[words_count]meta[Title].str.count( )1 meta.head(3)# words_count 列代表单词个数Title words_count0Guardians of the Galaxy41Prometheus12Split13 Genre 频次统计下面统计电影 Genre 的频次1 vcdf[Genre].value_counts()下面显示电影 Genre 的 Top5 最高频为出现 50 次的 Action,Adventure,Sci-Fi 类次之为 48 次的 Drama 类1234567 vc.head()Action,Adventure,Sci-Fi50Drama48Comedy,Drama,Romance35Comedy32Drama,Romance31Name: Genre, dtype: int64展示 Top5 的饼状图1234importmatplotlib.pyplot as plt vc[:5].plot(kindpie)matplotlib.axes._subplots.AxesSubplotobjectat0x000001D65B114948 plt.show()到此这篇关于Python数据处理的三个实用技巧分享的文章就介绍到这了