如何从DataFrame中选择一列数据

查看数据

import pandas as pd
movie= pd.read_csv('movie.csv')
movie.head()

RankTitleGenreDescriptionDirector…Runtime (Minutes)RatingVotesRevenue (Millions)Metascore01Guardians of the GalaxyAction,Adventure,Sci-FiA group of intergalactic criminals are forced …James Gunn…1218.1757074333.1376.012PrometheusAdventure,Mystery,Sci-FiFollowing clues to the origin of mankind, a te…Ridley Scott…1247.0485820126.4665.023SplitHorror,ThrillerThree girls are kidnapped by a man with a diag…M. Night Shyamalan…1177.3157606138.1262.034SingAnimation,Comedy,FamilyIn a city of humanoid animals, a hustling thea…Christophe Lourdelet…1087.260545270.3259.045Suicide SquadAction,Adventure,FantasyA secret government agency recruits some of th…David Ayer…1236.2393727325.0240.0

5 rows × 12 columns

选择Genre这列,方法一:

选择Genre这列
movie['Genre']

#显示结果
0       Action,Adventure,Sci-Fi
1      Adventure,Mystery,Sci-Fi
2               Horror,Thriller
3       Animation,Comedy,Family
                 ...

996                      Horror
997         Drama,Music,Romance
998            Adventure,Comedy
999       Comedy,Family,Fantasy
Name: Genre, Length: 1000, dtype: object

选择Genre这列,方法二:

也可以通过属性的方式选取
movie.Genre

#显示结果
0       Action,Adventure,Sci-Fi
1      Adventure,Mystery,Sci-Fi
2               Horror,Thriller
3       Animation,Comedy,Family
                 ...

996                      Horror
997         Drama,Music,Romance
998            Adventure,Comedy
999       Comedy,Family,Fantasy
Name: Genre, Length: 1000, dtype: object

查看类型

#查看类型
type(movie['Genre'])
#pandas.core.series.Series

查看选取的列的名字

Genre_N = movie['Genre']
 # 查看选取的列的名字
Genre_N.name

显示结果
#'Genre'

单列Series转换为DataFrame

Genre_N.to_frame().head()

Genre0Action,Adventure,Sci-Fi1Adventure,Mystery,Sci-Fi2Horror,Thriller3Animation,Comedy,Family4Action,Adventure,Fantasy

Original: https://blog.csdn.net/weixin_48135624/article/details/113767872
Author: 缘 源 园
Title: 如何从DataFrame中选择一列数据

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/742973/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球