Pandas选择行列的十大技能

感谢博主,转载自:

https://blog.csdn.net/qq_38328378/article/details/81166518

http://www.datastudy.cc/article/ec8c50baa8fd93ea85432eb85fb34eee

 

目录

    技能1、选择一列

    技能2、选择多列

    技能3、根据一个行索引,选择出一行

    技能4、根据一个行序号,选择出从开始到这个序号的行

    技能5、根据两个行序号,选择出从第一个序号到第二个序号的行

    技能7、根据一个列序号,选择出从开始列到这个序号的所有列

    技能8、条件过滤

    技能9、根据行字符串索引,进行行选择

    技能10、根据行索引/行位置,列名/列位置,进行具体位置的值选


Pandas中行列选择的十大技能

    今天,我们来学习一下,Pandas中的关于行列选择的十大技能,这些技能,绝对是你使用Pandas的过程中,需要用到的,因为,你肯定也想像Excel一样,任性地操作Python中的数据框。

    

    

    先来导入我们的演示数据,这里你直接复制执行就可以了。

    

# import the pandas module import pandas as pd

# Create an example dataframe about a fictional army raw_data = {
    'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks', 'Dragoons',
    'Dragoons', 'Dragoons', 'Dragoons', 'Scouts', 'Scouts', 'Scouts', 'Scouts'],
    'company': ['1st', '1st', '2nd', '2nd', '1st', '1st', '2nd', '2nd','1st', '1st', '2nd', '2nd'],
    'deaths': [523, 52, 25, 616, 43, 234, 523, 62, 62, 73, 37, 35],
    'battles': [5, 42, 2, 2, 4, 7, 8, 3, 4, 7, 8, 9],
    'size': [1045, 957, 1099, 1400, 1592, 1006, 987, 849, 973, 1005, 1099, 1523],
    'veterans': [1, 5, 62, 26, 73, 37, 949, 48, 48, 435, 63, 345],
    'readiness': [1, 2, 3, 3, 2, 1, 2, 3, 2, 1, 2, 3],
    'armored': [1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1],
    'deserters': [4, 24, 31, 2, 3, 4, 24, 31, 2, 3, 2, 3],
    'origin': ['Arizona', 'California', 'Texas', 'Florida', 'Maine', 'Iowa', 'Alaska', 'Washington', 'Oregon', 'Wyoming', 'Louisana', 'Georgia']
} df = pd.DataFrame(
    raw_data, 
    columns = ['regiment', 'company', 'deaths', 'battles', 'size', 'veterans', 'readiness', 'armored', 'deserters', 'origin']

) df = df.set_index('origin') df.head()
 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
origin         
ArizonaNighthawks1st523510451114
CaliforniaNighthawks1st524295752024
TexasNighthawks2nd2521099623131
FloridaNighthawks2nd6162140026312
MaineDragoons1st43415927320

3

    技能1、选择一列

df['size']

输出结果:

origin
Arizona       1045 
California     957 
Texas         1099 
Florida       1400 
Maine         1592 
Iowa          1006 
Alaska         987 
Washington     849 
Oregon         973 
Wyoming       1005 
Louisana      1099 
Georgia       1523 
Name: size, dtype: int64

    技能2、选择多列

   

df[['size', 'veterans']]
 sizeveterans
origin  
Arizona10451
California9575
Texas109962
Florida140026
Maine159273
Iowa100637
Alaska987949
Washington84948
Oregon97348
Wyoming1005435
Louisana109963
Georgia1523345

 

    技能3、根据一个行索引,选择出一行

        

# Select all rows with the index label "Arizona"     
df.loc[:'Arizona']
 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
origin         
ArizonaNighthawks1st523510451114

 

    技能4、根据一个行序号,选择出从开始到这个序号的行

# Select every row up to 3 
df.iloc[:2]

 

 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
origin         
ArizonaNighthawks1st523510451114
CaliforniaNighthawks1st524295752024

    

    技能5、根据两个行序号,选择出从第一个序号到第二个序号的行

    df.iloc[1:2]

 

 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
origin         
CaliforniaNighthawks1st524295752024

 

    技能6、根据一个行序号,选择出从这个行序号开始到结束的行

df.iloc[2:]
 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
origin         
TexasNighthawks2nd2521099623131
FloridaNighthawks2nd6162140026312
MaineDragoons1st434159273203
IowaDragoons1st2347100637114
AlaskaDragoons2nd52389879492024
WashingtonDragoons2nd623849483131
OregonScouts1st62497348202
WyomingScouts1st7371005435103
LouisanaScouts2nd378109963212
GeorgiaScouts2nd3591523345313

 

    技能7、根据一个列序号,选择出从开始列到这个序号的所有列

    

 # Select the first 2 columns
    df.iloc[:,:2]
 regimentcompany
origin  
ArizonaNighthawks1st
CaliforniaNighthawks1st
TexasNighthawks2nd
FloridaNighthawks2nd
MaineDragoons1st
IowaDragoons1st
AlaskaDragoons2nd
WashingtonDragoons2nd
OregonScouts1st
WyomingScouts1st
LouisanaScouts2nd
GeorgiaScouts2nd

 

    技能8、条件过滤

 # Select rows where df.deaths is greater than 50
    df[df['deaths'] > 50]
 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
origin         
ArizonaNighthawks1st523510451114
CaliforniaNighthawks1st524295752024
FloridaNighthawks2nd6162140026312
IowaDragoons1st2347100637114
AlaskaDragoons2nd52389879492024
WashingtonDragoons2nd623849483131
OregonScouts1st62497348202
WyomingScouts1st7371005435103
# Select rows where df.deaths is greater than 500 or less than 50 
df[(df['deaths'] > 500) | (df['deaths'] < 50)]
 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
origin         
ArizonaNighthawks1st523510451114
TexasNighthawks2nd2521099623131
FloridaNighthawks2nd6162140026312
MaineDragoons1st434159273203
AlaskaDragoons2nd52389879492024
LouisanaScouts2nd378109963212
GeorgiaScouts2nd3591523345313
# Select all the regiments not named "Dragoons" 
df[~(df['regiment'] == 'Dragoons')]
 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
origin         
ArizonaNighthawks1st523510451114
CaliforniaNighthawks1st524295752024
TexasNighthawks2nd2521099623131
FloridaNighthawks2nd6162140026312
OregonScouts1st62497348202
WyomingScouts1st7371005435103
LouisanaScouts2nd378109963212
GeorgiaScouts2nd3591523345313

 

    技能9、根据行字符串索引,进行行选择

    # Select the rows called Texas and Arizona
    df.ix[['Arizona', 'Texas']]

 

 regimentcompanydeathsbattlessizeveteransreadinessarmoreddeserters
ArizonaNighthawks1st523510451114
TexasNighthawks2nd2521099623131

    技能10、根据行索引/行位置,列名/列位置,进行具体位置的值选

# Select the third cell in the row named Arizona 
df.ix['Arizona', 'deaths']

523

 

# Select the third cell in the row named Arizona 
df.ix['Arizona', 2]

 

523

# Select the third cell down in the column named deaths 
df.ix[2, 'deaths']

25

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值