概述
import pandas as pd
import numpy as np
pd.options.display.max_columns = 40
#用列表选取多个列 dataframe[['列1','列2','列3','列4']]
movie = pd.read_csv('movie.csv')
movie_actor_director = movie[['Rank', 'Title', 'Genre', 'Director']]
movie_actor_director.head()
Rank | Title | Genre | Director | |
---|---|---|---|---|
0 | 1 | Guardians of the Galaxy | Action,Adventure,Sci-Fi | James Gunn |
1 | 2 | Prometheus | Adventure,Mystery,Sci-Fi | Ridley Scott |
2 | 3 | Split | Horror,Thriller | M. Night Shyamalan |
3 | 4 | Sing | Animation,Comedy,Family | Christophe Lourdelet |
4 | 5 | Suicide Squad | Action,Adventure,Fantasy | David Ayer |
选取单列 注意[[]]
# 选取单列 注意[[]] 返回的是DataFrame dataframe[]返回的是series
movie[['Director']].head()
Director | |
---|---|
0 | James Gunn |
1 | Ridley Scott |
2 | M. Night Shyamalan |
3 | Christophe Lourdelet |
4 | David Ayer |
将列表赋值给一个变量,便于多选
# 将列表赋值给一个变量,便于多选
cols=['Rank', 'Title', 'Genre', 'Director']
movie[cols]
Rank | Title | Genre | Director | |
---|---|---|---|---|
0 | 1 | Guardians of the Galaxy | Action,Adventure,Sci-Fi | James Gunn |
1 | 2 | Prometheus | Adventure,Mystery,Sci-Fi | Ridley Scott |
2 | 3 | Split | Horror,Thriller | M. Night Shyamalan |
... | ... | ... | ... | ... |
997 | 998 | Step Up 2: The Streets | Drama,Music,Romance | Jon M. Chu |
998 | 999 | Search Party | Adventure,Comedy | Scot Armstrong |
999 | 1000 | Nine Lives | Comedy,Family,Fantasy | Barry Sonnenfeld |
1000 rows × 4 columns
使用select_dtypes(),按类型选取列
# 使用select_dtypes(),选取浮点数列
movie.select_dtypes(include=['float']).head()
Rating | Revenue (Millions) | Metascore | |
---|---|---|---|
0 | 8.1 | 333.13 | 76.0 |
1 | 7.0 | 126.46 | 65.0 |
2 | 7.3 | 138.12 | 62.0 |
3 | 7.2 | 270.32 | 59.0 |
4 | 6.2 | 325.02 | 40.0 |
选取所有的数值列
# 选取所有的数值列
movie.select_dtypes(include=['number']).head()
Rank | Year | Runtime (Minutes) | Rating | Votes | Revenue (Millions) | Metascore | |
---|---|---|---|---|---|---|---|
0 | 1 | 2014 | 121 | 8.1 | 757074 | 333.13 | 76.0 |
1 | 2 | 2012 | 124 | 7.0 | 485820 | 126.46 | 65.0 |
2 | 3 | 2016 | 117 | 7.3 | 157606 | 138.12 | 62.0 |
3 | 4 | 2016 | 108 | 7.2 | 60545 | 270.32 | 59.0 |
4 | 5 | 2016 | 123 | 6.2 | 393727 | 325.02 | 40.0 |
通过filter()函数过滤选取多列
# 通过filter()函数过滤选取多列
movie.filter(like='Year').head()
Year | |
---|---|
0 | 2014 |
1 | 2012 |
2 | 2016 |
3 | 2016 |
4 | 2016 |
通过正则表达式选取多列 regex(Regular Expression)
# 通过正则表达式选取多列 regex(Regular Expression)
movie.filter(regex='s').head()
Runtime (Minutes) | Revenue (Millions) | |
---|---|---|
0 | 121 | 333.13 |
1 | 124 | 126.46 |
2 | 117 | 138.12 |
3 | 108 | 270.32 |
4 | 123 | 325.02 |
filter()函数,传递列表到参数items,选取多列
# filter()函数,传递列表到参数items,选取多列
movie.filter(items=['Year', 'Title']).head()
Year | Title | |
---|---|---|
0 | 2014 | Guardians of the Galaxy |
1 | 2012 | Prometheus |
2 | 2016 | Split |
3 | 2016 | Sing |
4 | 2016 | Suicide Squad |
最后
以上就是健壮乐曲为你收集整理的DataFrame基本操作如何取列的全部内容,希望文章能够帮你解决DataFrame基本操作如何取列所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复