约定
import pandas as pd
from pandas import DataFrame
import numpy as np
DataFrame
DataFrame是一个表格型的数据结构,既有行索引(保存在index)又有列索引(保存在columns)。
一、DataFrame对象常用属性:
- 创建DateFrame方法有很多(后面再介绍),最常用的是直接传入一个由等长列表或Numpy组成的字典:
dict1={
"Province":["Guangdong","Beijing","Qinghai","Fujiang"],
"year":[2018]*4,
"pop":[1.3,2.5,1.1,0.7]}
df1=DataFrame(dict1)
df1
代码结果:
Province | pop | year | |
---|---|---|---|
0 | Guangdong | 1.3 | 2018 |
1 | Beijing | 2.5 | 2018 |
2 | Qinghai | 1.1 | 2018 |
3 | Fujiang | 0.7 | 2018 |
- 同Series一样,也可在创建时指定序列(对于字典中缺失的用NaN):
df2=DataFrame(dict1,columns=['year','Province','pop','debt'],index=['one','two','three','four'])
df2
代码结果:
year | Province | pop | debt | |
---|---|---|---|---|
one | 2018 | Guangdong | 1.3 | NaN |
two | 2018 | Beijing | 2.5 | NaN |
three | 2018 | Qinghai | 1.1 | NaN |
four | 2018 | Fujiang | 0.7 | NaN |
- 同Series一样,DataFrame的index和columns有name属性:
df2
代码结果:
year | Province | pop | debt |
---|