pandas-数据索引和修改

pandas-数据索引和修改

数据选取

  1. 行列过滤:选取指定的行或者列
  2. 条件过滤:对列的数据设置过滤条件
  3. 函数过滤:通过函数设置更加复杂的过滤条件
import pandas as pd 
   
data_list = [['A0', 'B0', 'C0', 'D0', 'E0', 'F0', 'G0', 'H0', ],
             ['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1',],
             ['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', ],
             ['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', ],
             ['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', ]]
             
# 生成 DataFrame
data = pd.DataFrame(data_list, columns=list('ABCDEFGH')) 
data.index = ['zero','one','two','three','four']
print(data)
# 执行结果
        A   B   C   D   E   F   G   H
zero   A0  B0  C0  D0  E0  F0  G0  H0
one    A1  B1  C1  D1  E1  F1  G1  H1
two    A2  B2  C2  D2  E2  F2  G2  H2
three  A3  B3  C3  D3  E3  F3  G3  H3
four   A4  B4  C4  D4  E4  F4  G4  H4

列选择

# 方法1: DataFrame.column, 数据在前,列名在后,中间使用‘.’来连接
print(data.A)

# 方法2: DataFrame[columns], 数据在前,后跟列名,列名使用中括号包裹  
print(data['A'])

zero     A0
one      A1
two      A2
three    A3
four     A4
Name: A, dtype: object
# 选择多个列
print(data[['A','D','E']])
        A   D   E
zero   A0  D0  E0
one    A1  D1  E1
two    A2  D2  E2
three  A3  D3  E3
four   A4  D4  E4

行索引

# 方法.1 直接index 索引

# 左闭右开区间,包含 0,但不包含 1,所以只表示 1 行。
print(data[0:1])

       A   B   C   D   E   F   G   H
zero  A0  B0  C0  D0  E0  F0  G0  H0


# 选择多行
print(data[1:4])

        A   B   C   D   E   F   G   H
one    A1  B1  C1  D1  E1  F1  G1  H1
two    A2  B2  C2  D2  E2  F2  G2  H2
three  A3  B3  C3  D3  E3  F3  G3  H3

# 方法2 loc函数通过标签索引选择 行数据

同时行列索引

当需要同时选取行和列时,就需要同时使用行索引和列索引,或者同时使用列序号和行序号。对应的方法有 lociloc

DataFrame.iloc

iloc 方法的参数值是行的整数序号,和列的整数序号

参数可以是索引值,索引列表,索引范围

data.index = ['zero','one','two','three','four']
print(data)
        A   B   C   D   E   F   G   H
zero   A0  B0  C0  D0  E0  F0  G0  H0
one    A1  B1  C1  D1  E1  F1  G1  H1
two    A2  B2  C2  D2  E2  F2  G2  H2
three  A3  B3  C3  D3  E3  F3  G3  H3
four   A4  B4  C4  D4  E4  F4  G4  H4

# 选择行
#  行范围参数和列范围参数之间使用逗号分隔 
print(data.iloc[[1,4], :])
       A   B   C   D   E   F   G   H
one   A1  B1  C1  D1  E1  F1  G1  H1
four  A4  B4  C4  D4  E4  F4  G4  H4


# 选择列
print(data.iloc[:, 2:6])
        C   D   E   F
zero   C0  D0  E0  F0
one    C1  D1  E1  F1
two    C2  D2  E2  F2
three  C3  D3  E3  F3
four   C4  D4  E4  F4

# 同时选择行,同时选择列
print(data.iloc[1:4, 2])
one      C1
two      C2
three    C3
Name: C, dtype: object

# 同时选择行,同时选择列
print(data.iloc[1, 0])
A1

# 获取 data 中,行索引在列表 [0,2,4] 中,列名称在列表 ['B','D','H'] 中的所有数据。
print(data.iloc[[0,2,4], [1,2,4]])
       B   C   E
zero  B0  C0  E0
two   B2  C2  E2
four  B4  C4  E4

# 获取 data 中,行索引在列表 [0,2,4] 中,列名称为 F 的所有数据。
print(data.iloc[[0,2,4], 5])

0    F0
2    F2
4    F4
Name: F, dtype: object


DataFrame.loc

loc 方法接收两个参数,参数之间用逗号分隔。其中第一个参数表示行的范围,第二个参数表示列的范围。

#  行索引使用范围参数
import pandas as pd
data_list = [['A0', 'B0', 'C0', 'D0', 'E0', 'F0', 'G0', 'H0', ],
             ['A1', 'B1', 'C1', 'D1', 'E1', 'F1', 'G1', 'H1',],
             ['A2', 'B2', 'C2', 'D2', 'E2', 'F2', 'G2', 'H2', ],
             ['A3', 'B3', 'C3', 'D3', 'E3', 'F3', 'G3', 'H3', ],
             ['A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', ]]
             
# 生成 DataFrame
data = pd.DataFrame(data_list, columns=list('ABCDEFGH')) 
data.index = ['zero','one','two','three','four']
# print(data)

# 选取指定的行
print(data.loc["zero":"one", :])
       A   B   C   D   E   F   G   H
zero  A0  B0  C0  D0  E0  F0  G0  H0
one   A1  B1  C1  D1  E1  F1  G1  H1

# 按范围选取列
print(data.loc[:, ["A","D"]])
        A   D
zero   A0  D0
one    A1  D1
two    A2  D2
three  A3  D3
four   A4  D4

# 行和列也可以同时设置
print(data.loc[["zero","two","four"], "B":"E"])
       B   C   D   E
zero  B0  C0  D0  E0
two   B2  C2  D2  E2
four  B4  C4  D4  E4

修改数据

import pandas as pd
data = {
    'state': ['python', 'python', 'go', 'java'],
    'year': [2001, 2002, 2001, 2002],
    'pop': [1.7, 3.6, 2.4, 2.9]
}
frame = pd.DataFrame(data)
print(frame)

#     state  year  pop
# 0  python  2001  1.7
# 1  python  2002  3.6
# 2      go  2001  2.4
# 3    java  2002  2.9

# 修改单一数据
frame.loc[2,"pop"]=5.0

# 修改整列数据
frame.loc[:,"year"]=[2018, 2019, 2020, 2021]
print(frame)
    state  year  pop
0  python  2018  1.7
1  python  2019  3.6
2      go  2020  5.0
3    java  2021  2.9

# 修改行数据
frame.loc[0,:]=["c++", 1998, 2.1]

# 将一个值赋值给所选区域
frame.iloc[:,2:] = 'same'
print(frame)

#     state  year   pop
# 0     c++  1998  same
# 1  python  2005  same
# 2      go  2016  same
# 3    java  2023  same

# 将精确修改区域内所有数据
frame.iloc[[0, 2], [0, 2]] = [["rust", 4.1],
                              ["sql", 0.3]]
print(frame) 	

# 1  python  2005  same
# 2     sql  2016   0.3
# 3    java  2023  same

参考资料

条件过滤

import pandas as pd
data = {
    'state': ['python', 'python', 'go', 'java'],
    'year': [2001, 2005, 2016, 2023],
    'pop': [1.7, 3.6, 2.4, 2.9]
}
frame = pd.DataFrame(data)
print(frame)

#     state  year  pop
# 0  python  2001  1.7
# 1  python  2005  3.6
# 2      go  2016  2.4
# 3    java  2023  2.9

print(frame["year"]>2010)
#0    False
#1    False
#2     True
#3     True

df=frame[frame["year"]>2010]
print(df)

#  state  year  pop
#2    go  2016  2.4
#3  java  2023  2.9

参考资料

https://www.cnblogs.com/wang_yb/p/17388485.html

参考资料1

参考资料2

posted @ 2023-08-25 17:48  贝壳里的星海  阅读(113)  评论(0编辑  收藏  举报