series学习代码
"""
pandas 入门
Author:sonny
Date:2019/06
"""
import pandas as pd
"""pandas两种数据结构:Series,DataFrame"""
from pandas import Series
"""Series初始化方式:通过列表初始化,通过字典初始化"""
list_series = Series([1,2,3])
dic_series = Series({'Beijing':100,'shanghai':200})
"""查看Series对象的键与值"""
print(list_series)
print(list_series.index)
print(list_series.values)
"""添加Series的index,通过index去检索"""
obj = Series([4,7,-5,3],index = ['a','b','c','d'])
print (obj)
print ("obj['a']:",obj['a'])
print ("obj[['c','a','d']]:\n",obj[['c','a','d']])
"""Series数组运算"""
import numpy as np
print(obj[obj>0])
print(obj*2)
print(np.exp(obj))
"""查看键是否存在"""
print("'b' in obj:",'b' in obj)
print("'e' in obj:",'e' in obj)
"""通过index修改Series的键,匹配的键才会有值"""
sdata = {'Beijing':100,'shanghai':200}
obj2 = Series(sdata,index = ['shenzhen','guangzhou','Beijing'])
print(obj2)
"""Series自动对齐数据,相同索引上的元素直接相加,索引不同的元素默认为NA值"""
series_1 = Series({'num1':1,'num2':2})
series_2 = Series({'num1':1,'num3':3})
print(series_1+series_2)
"""Series对象名称的添加,index名称的添加"""
series_1 = Series([1000,2000,3000],\
index = ['person_1','person_2','person_3'])
print("series_1.name:{}, series_1.index.name:{}"\
.format(series_1.name,series_1.index.name))
series_1.name = "salary"
series_1.index.name = "person_name"
print(series_1)
print("series_1.name:{}, series_1.index.name:{}"\
.format(series_1.name,series_1.index.name))
"""索引的修改"""
series_1 = Series([1,2,3],index = ['a','b','c'])
series_1.index = ['d','e','f']
print(series_1)
part2:dataFrame学习代码
"""
pandas入门,dataFrame基础
Author:sonny
date:2019/06
"""
import pandas as pd
from pandas import DataFrame
import numpy as np
"""
DataFrame与Series相比,面向行和列的操作是平衡的,
数据多以一个或者多个二维块存放
通过传递一个 numpyarray,时间索引以及列标签来创建一个DataFrame
"""
dates = pd.date_range('20130101', periods=6)
print(dates)
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))
print(df)
"""通过传递一个能够被转换成类似序列结构的字典对象来创建一个DataFrame"""
df2 = pd.DataFrame({'A' : 1.,
'B' : pd.Timestamp('20130102'),
'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
'D' : np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["test","train","test","train"]),
'F' : 'foo' })
print(df2)
"""查看不同列的数据类型"""
print(df2.dtypes)
"""查看DataFrame中头部和尾部的行"""
print(df.head(3))
print(df.tail(3))
"""显示索引、列和底层的 numpy 数据"""
print(df.index)
print(df.columns)
print(df.values)
""" describe()函数对于数据的快速统计汇总"""
print(df.describe())
"""对数据的转置"""
print(df.T)
"""按轴进行排序,axis =0为按列,1为按行,ascending = False为降序"""
print(df.sort_index(axis=0))
print(df.sort_index(axis=1))
print(df.sort_index(axis=1, ascending=False))
print(df.sort_index(axis=0, ascending=False))
"""按值进行排序"""
print(df.sort_values(by='B'))
"""选择一个单独的列,这将会返回一个Series,等同于df.A"""
print(df['A'])
""" 通过[]进行选择,这将会对行进行切片"""
print(df[0:3])
print(df['20130102':'20130104'])
"""使用标签来获取一个交叉的区域"""
print(df.loc[dates[0]])
print(df.loc[:,'A'])
print(df.loc['20130102':'20130104'])
"""通过标签来在多个轴上进行选择"""
print(df.loc[:,['A','B']])
print(df.loc['20130102':'20130104',['A','B']])
"""标签切片"""
print(df.loc['20130102':'20130104',['A','B']])
"""对于返回的对象进行维度缩减"""
print(df.loc['20130102',['A','B']])
"""获取一个标量"""
print(df.loc[dates[0],'A'])
"""快速访问一个标量(与上一个方法等价)"""
print(df.at[dates[0],'A'])
"""通过传递数值进行位置选择(选择的是行)"""
print(df.iloc[3])
"""通过数值进行切片,与 numpy/python 中的情况类似"""
print(df.iloc[3:5,0:2])
"""通过指定一个位置的列表,与 numpy/python 中的情况类似"""
print(df.iloc[[1,2,4],[0,2]])
"""对行进行切片"""
print(df.iloc[1:3,:])
"""对列进行切片"""
print(df.iloc[:,1:3])
"""获取特定的值"""
print(df.iloc[1,1])
"""快速访问标量(等同于前一个方法)"""
print(df.iat[1,1])
"""布尔索引,使用一个单独列的值来选择数据"""
print(df[df.A > 0])
"""使用where操作来选择数据"""
print(df[df > 0])
""" 使用isin()方法来过滤:"""
df2 = df.copy()
df2['E'] = ['one', 'one','two','three','four','three']
print(df2)
print(df2['E'].isin(['two']))
print(df2[df2['E'].isin(['two','four'])])