DataFrame基本操作如何取列

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()

RankTitleGenreDirector01Guardians of the GalaxyAction,Adventure,Sci-FiJames Gunn12PrometheusAdventure,Mystery,Sci-FiRidley Scott23SplitHorror,ThrillerM. Night Shyamalan34SingAnimation,Comedy,FamilyChristophe Lourdelet45Suicide SquadAction,Adventure,FantasyDavid Ayer

选取单列 注意[[]]

选取单列 注意[[]] 返回的是DataFrame dataframe[]返回的是series
movie[['Director']].head()

Director0James Gunn1Ridley Scott2M. Night Shyamalan3Christophe Lourdelet4David Ayer

将列表赋值给一个变量,便于多选

将列表赋值给一个变量,便于多选
cols=['Rank', 'Title', 'Genre', 'Director']

movie[cols]

RankTitleGenreDirector01Guardians of the GalaxyAction,Adventure,Sci-FiJames Gunn12PrometheusAdventure,Mystery,Sci-FiRidley Scott23SplitHorror,ThrillerM. Night Shyamalan……………997998Step Up 2: The StreetsDrama,Music,RomanceJon M. Chu998999Search PartyAdventure,ComedyScot Armstrong9991000Nine LivesComedy,Family,FantasyBarry Sonnenfeld

1000 rows × 4 columns

使用select_dtypes(),按类型选取列

使用select_dtypes(),选取浮点数列
movie.select_dtypes(include=['float']).head()

RatingRevenue (Millions)Metascore08.1333.1376.017.0126.4665.027.3138.1262.037.2270.3259.046.2325.0240.0

选取所有的数值列

选取所有的数值列
movie.select_dtypes(include=['number']).head()

RankYearRuntime (Minutes)RatingVotesRevenue (Millions)Metascore0120141218.1757074333.1376.01220121247.0485820126.4665.02320161177.3157606138.1262.03420161087.260545270.3259.04520161236.2393727325.0240.0

通过filter()函数过滤选取多列

通过filter()函数过滤选取多列
movie.filter(like='Year').head()

Year0201412012220163201642016

通过正则表达式选取多列 regex(Regular Expression)

通过正则表达式选取多列 regex(Regular Expression)
movie.filter(regex='\s').head()

Runtime (Minutes)Revenue (Millions)0121333.131124126.462117138.123108270.324123325.02

filter()函数,传递列表到参数items,选取多列

filter()函数,传递列表到参数items,选取多列
movie.filter(items=['Year', 'Title']).head()

YearTitle02014Guardians of the Galaxy12012Prometheus22016Split32016Sing42016Suicide Squad

Original: https://blog.csdn.net/weixin_48135624/article/details/113824385
Author: 缘 源 园
Title: DataFrame基本操作如何取列

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

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

(0)

大家都在看

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