Pandas入门系列(五) -- Indexing和Selecting

数据分析汇总学习

https://blog.csdn.net/weixin_39778570/article/details/81157884

import pandas as pd
import numpy as np
from pandas import Series, DataFrame
# df1.csv文件见上一篇
In [34]: df = pd.read_csv('df1.csv')

In [35]: df
Out[35]: 
    Format     Type          Data     Description        Reader     Writer
0     text      CSV      read_csv          to_csv           NaN        NaN
1     text     JSON     read_json         to_json           NaN        NaN
2     text     HTML     read_html         to_html           NaN        NaN
3     text    Local     clipboard  read_clipboard  to_clipboard        NaN
4   binary       MS         Excel      read_excel      to_excel        NaN
5   binary     HDF5        Format        read_hdf        to_hdf        NaN
6   binary  Feather        Format    read_feather    to_feather        NaN
7   binary  Msgpack  read_msgpack      to_msgpack           NaN        NaN
8   binary    Stata    read_stata        to_stata           NaN        NaN
9   binary      SAS      read_sas             NaN           NaN        NaN
10  binary   Python        Pickle          Format   read_pickle  to_pickle
11     SQL      SQL      read_sql          to_sql           NaN        NaN
12     SQL   Google           Big           Query      read_gbq     to_gbq
# 支持索引操作
df['Format']
Out[36]: 
0       text
1       text
2       text
3       text
4     binary
5     binary
6     binary
7     binary
8     binary
9     binary
10    binary
11       SQL
12       SQL
Name: Format, dtype: object
df[['Format','Type']]
Out[37]: 
    Format     Type
0     text      CSV
1     text     JSON
2     text     HTML
3     text    Local
4   binary       MS
5   binary     HDF5
6   binary  Feather
7   binary  Msgpack
8   binary    Stata
9   binary      SAS
10  binary   Python
11     SQL      SQL
12     SQL   Google
# 使用iloc进行index切片操作,前一个切片表示行,后一个切片表示列
df.iloc[1:5,2:3]
Out[38]: 
        Data
1  read_json
2  read_html
3  clipboard
4      Excel
df1 = df.iloc[5:11, 1:5]

df1
Out[40]: 
       Type          Data   Description       Reader
5      HDF5        Format      read_hdf       to_hdf
6   Feather        Format  read_feather   to_feather
7   Msgpack  read_msgpack    to_msgpack          NaN
8     Stata    read_stata      to_stata          NaN
9       SAS      read_sas           NaN          NaN
10   Python        Pickle        Format  read_pickle

df1.iloc[2:3,:]
Out[41]: 
      Type          Data Description Reader
7  Msgpack  read_msgpack  to_msgpack    NaN

# 使用loc对内容进行挑选
n [42]: df.loc[1:10]
Out[42]: 
    Format     Type          Data     Description        Reader     Writer
1     text     JSON     read_json         to_json           NaN        NaN
2     text     HTML     read_html         to_html           NaN        NaN
3     text    Local     clipboard  read_clipboard  to_clipboard        NaN
4   binary       MS         Excel      read_excel      to_excel        NaN
5   binary     HDF5        Format        read_hdf        to_hdf        NaN
6   binary  Feather        Format    read_feather    to_feather        NaN
7   binary  Msgpack  read_msgpack      to_msgpack           NaN        NaN
8   binary    Stata    read_stata        to_stata           NaN        NaN
9   binary      SAS      read_sas             NaN           NaN        NaN
10  binary   Python        Pickle          Format   read_pickle  to_pickle

df.loc[1:10,:'Data']
Out[43]: 
    Format     Type          Data
1     text     JSON     read_json
2     text     HTML     read_html
3     text    Local     clipboard
4   binary       MS         Excel
5   binary     HDF5        Format
6   binary  Feather        Format
7   binary  Msgpack  read_msgpack
8   binary    Stata    read_stata
9   binary      SAS      read_sas
10  binary   Python        Pickle

df.loc[1:10,'Data':'Reader']
Out[44]: 
            Data     Description        Reader
1      read_json         to_json           NaN
2      read_html         to_html           NaN
3      clipboard  read_clipboard  to_clipboard
4          Excel      read_excel      to_excel
5         Format        read_hdf        to_hdf
6         Format    read_feather    to_feather
7   read_msgpack      to_msgpack           NaN
8     read_stata        to_stata           NaN
9       read_sas             NaN           NaN
10        Pickle          Format   read_pickle

你可能感兴趣的:(python数据科学,python,pandas,index,select,iloc)