Python Pandas DataFrame取列
1.取一列
首先我们建立一个dataframe结构:df
>>> import numpy as np
>>> import pandas as pd
>>> dfd = {11:["a","b","c"],22:["d","e","f"],33:["g","h","i"]}
>>> df = pd.DataFrame(dfd)
得到如下,列名为11,22,33的一个3*3矩阵
>>> df
11 22 33
0 a d g
1 b e h
2 c f i
以列名取列
>>> df_11 = df[11]
>>> df_11
0 a
1 b
2 c
Name: 11, dtype: object
以列号取列(第几列)
使用iloc方法,iloc[row行,column列],取一整列的意思就是使列一样,把这一列里的每一行都取出来。
>>> df_22 = df.iloc[:,1]
>>> df_22
0 d
1 e
2 f
Name: 22, dtype: object
2.取多列
以列名取多列
>>> df_11_33 = df[[11,33]]
>>> df_11_33
11 33
0 a g
1 b h
2 c i
以列号(第几列)
>>> kk = df.iloc[:,[0,2]]
>>> kk
11 33
0 a g
1 b h
2 c i