1. 程式人生 > 實用技巧 >整理Pandas讀取行列資料方法

整理Pandas讀取行列資料方法

1、讀取方法有按行,按列;按位置(座標),按字元(索引);函式有 df.iloc(), df.loc(), df.iat(), df.at(), df.ix()。

2、轉換為DF,賦值columns,index,修改新增資料,取行列索引

data = {'省份': ['北京', '上海', '廣州', '深圳'],
        '年份': ['2017', '2018', '2019', '2020'],
        '總人數': ['2200', '1900', '2170', '1890'],
        '高考人數': ['6.3', '5.9', '6.0', '5.2']}
df = pd.DataFrame(data, columns=['
省份', '年份', '總人數', '高考人數', '高數'], index=['one', 'two', 'three', 'four']) df['高數'] = ['90', '95', '92', '98'] print("行索引:{}".format(list(df.index))) print("列索引:{}".format(list(df.columns))) print(df.index[1:3]) print(df.columns[1]) print(df.columns[1:3]) print(df)
行索引:['one', 'two', '
three', 'four'] 列索引:['省份', '年份', '總人數', '高考人數', '高數'] Index(['two', 'three'], dtype='object') 年份 Index(['年份', '總人數'], dtype='object') 省份 年份 總人數 高考人數 高數 one 北京 2017 2200 6.3 90 two 上海 2018 1900 5.9 95 three 廣州 2019 2170 6.0 92 four 深圳 2020 1890 5.2 98

3、

print(df['省份'])  #按列名取列
print(df.省份) #按列名取列 print(df[['省份', '總人數']]) #按列名取列 print(df[df.columns[1:4]]) #按列索引取列 print(df.iloc[:, 1]) #按位置取列 print(df.iloc[:, [1, 3]]) #按位置取列
one      北京
two      上海
three    廣州
four     深圳
Name: 省份, dtype: object
one      北京
two      上海
three    廣州
four     深圳
Name: 省份, dtype: object
       省份   總人數
one    北京  2200
two    上海  1900
three  廣州  2170
four   深圳  1890
         年份   總人數 高考人數
one    2017  2200  6.3
two    2018  1900  5.9
three  2019  2170  6.0
four   2020  1890  5.2
one      2017
two      2018
three    2019
four     2020
Name: 年份, dtype: object
         年份 高考人數
one    2017  6.3
two    2018  5.9
three  2019  6.0
four   2020  5.2

4、

print(df.iloc[1])
print(df.iloc[1, 3])
print(df.iloc[[1], [3]])
print(df.loc[df.index[1:3]])  #按行索引取行,但沒必要
print(df.iloc[1:3])
print(df.iloc[[1, 3]])
print(df.iloc[[1,2,3], [2,4]])
省份        上海
年份      2018
總人數     1900
高考人數     5.9
高數        95
Name: two, dtype: object
5.9
    高考人數
two  5.9
       省份    年份   總人數 高考人數  高數
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
       省份    年份   總人數 高考人數  高數
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
      省份    年份   總人數 高考人數  高數
two   上海  2018  1900  5.9  95
four  深圳  2020  1890  5.2  98
        總人數  高數
two    1900  95
three  2170  92
four   1890  98

5、

print(df.loc['two'])
print(df.loc['two', '省份'])
print(df.loc['two':'three'])
print(df.loc[['one', 'three']])
print(df.loc[['one', 'three'], ['省份', '年份']])
省份        上海
年份      2018
總人數     1900
高考人數     5.9
高數        95
Name: two, dtype: object
上海
       省份    年份   總人數 高考人數  高數
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
       省份    年份   總人數 高考人數  高數
one    北京  2017  2200  6.3  90
three  廣州  2019  2170  6.0  92
       省份    年份
one    北京  2017
three  廣州  2019

6、

print(df.ix[1:3])
print(df.ix[:, [1, 3]])
print(df.iat[1,3])
print(df.at['two', '省份'])
       省份    年份   總人數 高考人數  高數
two    上海  2018  1900  5.9  95
three  廣州  2019  2170  6.0  92
         年份 高考人數
one    2017  6.3
two    2018  5.9
three  2019  6.0
four   2020  5.2
5.9
上海