python 使用 DataFrame

Pandas 的数据结构主要是:Series(一维数组),DataFrame(二维数组)。DataFrame 是由索引和内容组成,索引既有行索引 index 又有列索引 columns,如 内容,index=[],colunms=[] 这样的形式。

1 Pandas 中创建 DataFrame
1.1 pd.DataFrame(ndarray 数据,index=[‘行索引 1’,‘行索引 2’],colunms=[‘列索引 1’,‘列索引 2’])

import numpy as np
import pandas as pd
a=pd.DataFrame(np.arange(18).reshape(3,6),index=[‘a’,‘b’,‘c’],columns=[‘A’,‘B’,‘C’,‘D’,‘E’,‘F’])
print(a)

A B C D E F

a 0 1 2 3 4 5

b 6 7 8 9 10 11

c 12 13 14 15 16 17

1.2 pd.DataFrame(dict 数据)

a=pd.DataFrame([{‘a’:0,‘b’:3,‘c’:6},{‘a’:1,‘b’:4,‘c’:7},{‘a’:2,‘b’:8,‘c’:5}]) #带字典的列表
b=pd.DataFrame({‘a’:[0,1,2],‘b’:[3,4,8],‘c’:[6,7,5]}) #字典
c=pd.DataFrame(dict(a=[0,1,2],b=[3,4,5],c=[6,7,8])) #字典
#out: a b c

0 0 3 6

1 1 4 7

2 2 8 5

2 DataFrame 属性
2.1 df.shape :查看 DataFrame 的形状

2.2 df.dtypes:返回 DataFrame 的列数据类型

2.3 df[df.index== 某行索引值]:对某行内容进行索引

import pandas as pd
A=pd.DataFrame({‘a’:[1,2,3],‘b’:[2,3,4],‘c’:[2,4,5]})
print(A[A.index==0])
#Out:a b c

0 1 2 2

print(A[A.a==1])
#Out:a b c

0 1 2 2

2.4 df.columns:列索引

2.5 df.head():仅显示前面几行数据(默认是前五行)

2.6 df.tail():仅显示最后几行数据(默认是后五行)

2.7 dataframe.values:仅返回数据框中的值,轴标签将被删除

2.8 dataframe.astype: 对 DataFrame 的列数据类型转换

df = pd.DataFrame({‘age’: [ 3, 29],
‘height’: [94, 170],
‘weight’: [31, 115]})
#df
age height weight
0 3 94 31
1 29 170 115
#df.dtypes
age int64
height int64
weight int64
dtype: object
#df.values
array([[3, 94, 31],
[29, 170, 115]])
3 DataFrame 的索引
3.1 查看某列——df[‘列索引’]

import pandas as pd
A=pd.DataFrame(dict(a=[1,2,3],b=[2,3,4],c=[23,5,2]))
print(A[‘a’])
#Out:0 1

1 2

2 3

Name: a, dtype: int64

3.2 查看某个数据——df[‘列索引’]['行索引']

import pandas as pd
import numpy as np
B=pd.DataFrame(np.arange(12).reshape(3,4),index=[‘a’,‘b’,‘c’],columns=[‘AA’,‘BB’,‘CC’,‘DD’])
print(B[‘AA’][0])
#Out:0
需要注意的是想要查看某行,不能用 df[‘行索引’] 这样的形式。因为在 Pandas 中方括号写数组, 表示取行索引对行进行操作,方括号写字符串,表示取列索引对列进行操作。

3.2 查看某行——df.loc[] 函数和 df.iloc[] 函数

import pandas as pd
import numpy as np
a=pd.DataFrame(np.arange(15).reshape(3,5),index=[‘a’,‘b’,‘c’],columns=[‘aa’,‘bb’,‘cc’,‘dd’,‘ee’])
print(a)
#Out1: aa bb cc dd ee

a 0 1 2 3 4

b 5 6 7 8 9

c 10 11 12 13 14

print(a.loc[‘a’:‘b’,‘aa’:‘dd’])
#Out2: aa bb cc dd

a 0 1 2 3

b 5 6 7 8

print(a.iloc[0:1,0:3])
#Out3: aa bb cc

a 0 1 2

3.2.1 df.loc[索引] 函数:方框 +‘索引’,闭区间

3.2.2 df.iloc[下标] 函数:方框 + 下标,左到右不到

在读取 Series 的数据时,就有通过方括号 + 索引 / 下标值的方式读取对应数据这两种方式。这是因为 Series 的索引是可以重新定义的,而下标始终是不变的(0 开始)。

3.4 查看行数据时还可以以下方式

df[df.index== 某一行索引值]:查看某行数据

df[df. 某列索引名 == 该列的一个值]:对一行或多行内容进行索引