import numpy as np
import pandas as pd
from pandas import Series, DataFrame
a = np.random.randn(5)print("a is an array:")print(a)
s = Series(a)# Series 可以简单地被认为是一维的数组# Series 和一维 数组最主要的区别在于 Series 类型具有索引( index )print("s is a Series:")print(s)print(s[2])
s = Series(np.random.randn(5), index=['a','b','c','d','e'], name='my_series')print(s)print(s.index)print(s.name)print('----------------------')
d ={'a':0.,'b':1,'c':2}print("d is a dict:")print(d)
s = Series(d)print("s is a Series:")print(s)# NaN (not a number, pandas 中数据缺失的标准记号)# DataFrame是将数个Series按列合并而成的二维数据结构,每一列单独取出来是一个Seriesprint('-----------DataFrame()---------')
d ={'one': Series([1.,2.,3.], index=['a','b','c']),'two': Series([1.,2.,3.,4.], index=['a','b','c','d'])}
df = DataFrame(d)print(df)
df = DataFrame(d, index=['r','d','a'], columns=['two','three'])print(df)print(df.index)print(df.columns)print(df.values)print('---------------------------')
d ={'one':[1.,2.,3.,4.],'two':[4.,3.,2.,1.]}
df = DataFrame(d, index=['a','b','c','d'])print(df)print('++++++++++++++++++++++++')
a = Series(range(5))print(a)
b = Series(np.linspace(4,20,5))print(b)
df = pd.concat([a, b], axis=1)# 的 axis=1 表示按列进行合并,axis=0 表示按行合并print(df)print('*************************')# df = pd.concat([a, b], axis=0) # 的 axis=1 表示按列进行合并,axis=0 表示按行合并# print(df)# DataFrame是以列作为操作的基础的,全部操作都想象成先从DataFrame# 里取一列,再从这个Series取元素即可print('######################')
df = DataFrame()
index =['alpha','beta','gamma','delta','eta']for i inrange(5):
a = DataFrame([np.linspace(i,5* i,5)], index=[index[i]])
df = pd.concat([df, a], axis=0)print(df)print('************************')
df.columns =['a','b','c','d','e']print(df)print('取出一列数据')print(df['b'])# 列print(df.a)print('访问特定数据')print(df['b'][2])print(df['b']['gamma'])print('取出一行数据')print(df.iloc[1])print(df.loc['beta'])print('&&&&&&&&&&&&&&&&&&&&')print("Selecting by slices:")print(df[1:3])