Series
、DataFrame
import pandas as pd
obj = pd.Series([4, 7, -5, 3])
obj
"""
输出
0 4
1 7
2 -5
3 3
dtype: int64
"""
obj.values
"""
输出
array([ 4, 7, -5, 3], dtype=int64)
"""
obj.index
"""
输出
RangeIndex(start=0, stop=4, step=1)
"""
obj2 = pd.Series([4, 7], index=['d', 'e'])
obj2
"""
输出
d 4
e 7
dtype: int64
"""
obj2['e']
"""
输出
7
"""
obj2[['e','d']]
"""
输出
e 7
d 4
dtype: int64
"""
sdata = {'ohio': 35000, 'tesas': 71000}
obj3 = pd.Series(sdata)
obj3
"""
输出
ohio 35000
tesas 71000
dtype: int64
"""
states = ['califor', 'tesas', 'ohio']
obj4 = pd.Series(sdata, index=states)
obj4
"""
输出
califor NaN
tesas 71000.0
ohio 35000.0
dtype: float64
"""
DataFrame 是一个表格型的数据结构,它包含有一组有序的列,每列可以是不同的值类型。
DataFrame 既有行索引也有列索引。DataFrame 中的数据是以一个或多个 二维块存放的。
data = {'state': ['ohio','ohio','nevada','nevada'],
'year': [2000,2001,2001,2002],
'pop': [1.5,1.7,2.4,2.9]}
frame = pd.DataFrame(data)
frame
frame2 = pd.DataFrame(data, columns=['year','state','pop', 'debt'], index=['one','two','three','four'])
frame2
frame2['state']
frame2.loc['one']
val = pd.Series([-1.2,-1.5], index=['two','four'])
frame2['debt'] = val
frame2
pop = {'ohio':{'2000':1.5,'2001':1.7}, 'nevada':{'2001':2.4}}
frame3 = pd.DataFrame(pop)
frame3
obj3 = pd.Series(['blue', 'purple', 'yellow'], index=[0, 2, 4])
obj3
obj3.reindex(range(6), method='ffill')