第一个问题可以在pandas官方网站链接: link 找到答案
pandas is an open source BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.
从这一段话中我们可以知道如下信息:
第二个问题
How to use Pandas?
要理解pandas的功能,就必须先了解pandas创建出对象的数据类型
Series类型由一组数据及与之相关的数据索引组成,用于一维数组
简称:一组索引,一组值
DataFrame类型由共用相同索引的一组列组成,用于二维或多维数组
简称:一组索引,多组值
import pandas as pd
#列表创建
#注:顺序不可调换:值在前,索引在后. index可省略
pd.Series([1,2,3,4])
pd.Series([1,2,3,4],index=[0,1,2,3])
pd.Series([1,2,3,4],[0,1,2,3])
#range函数创建
pd.Series(range(1,5,1))
#ndarray创建
pd.Series(np.array(range(1,5,1)))
#字典创建
#字典中的key相当于Series中的index
pd.Series({0:1,1:2,2:3,3:4})
#index参数可以指定索引顺序
pd.Series({0:1,1:2,2:3,3:4},index=[0,1,2,3])
#以上运行结果皆为(除np是int32以外)
Out[94]:
0 1
1 2
2 3
3 4
dtype: int64
#由Series&字典创建
pd.DataFrame({'one':pd.Series(range(1,3,1),['a','b']),'two':pd.Series([4,5],index=['a','b'])})
#由列表字典创建
pd.DataFrame({'one':[1,2],'two':[4,5]},index=['a','b'])
#上述两种创建方式结果均为:
Out[100]:
one two
a 1 4
b 2 5
#从二维ndarray对象创建
pd.DataFrame(np.arange(10).reshape(2,5),index=['a','b'],columns=['one','two','three','four','five'])
Out[103]:
one two three four five
a 0 1 2 3 4
b 5 6 7 8 9
#Series&DataFrame共有
n.values
n.index
#Series
n.name #需提前在创建时指定属性 n=np.Series(...,name='')
n.index.name
#DataFrame
n.columns
适用于Series和DataFrame
get #类似字典中的get方法
n.get(key, default=None)
key -- 字典中要查找的键。
default -- 如果指定键的值不存在时,返回该默认值。
in #判断值是否存在
存在返回True
不存在返回False
pandas数据类型存在两种索引类型:
数字索引( 从0开始 )
自定义索引
DataFrame类型既有行索引也有列索引
#行索引
n.index
#列索引
n.columns
#值索引
n[c][i]
Series类型只有行索引
自定义行或列的排序
n.reindex(index=None, columns=None, …)
index=[你希望重排后的顺序]
columns=[你希望重排后的顺序]
对行或列进行升序或降序排序
n.sort_index(axis=0,ascending = False)
axis 轴
#轴(axis): 保存数据的维度;秩(rank):轴的数量
axis=0 默认重排行
axis=1 重排列
ascending=False 升序(从小到大)
按值排序
DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')
#### 参数说明
axis=0 默认重排行
axis=1 重排列
by:str or list of str;
axis=0,by="列名"
axis=1,by="行名";
ascending=True 升序(从小到大)
inplace:布尔型,是否用排序后的数据框替换现有的数据框
kind:排序方法,{‘quicksort’, ‘mergesort’, ‘heapsort’}
默认是kind='quicksort'
na_position : {‘first’, ‘last’}
na_position='last'默认缺失值排在最后面
insert方法 索引类型方法 (作用对象为index类型)
#在loc位置增加一个元素e
d.index.insert(loc,e)
d.columns.insert(loc,e)
n = pd.DataFrame(np.arange(10).reshape(2,5),index=['a','b'],columns=['one','two','three','four','five'])
Out[103]:
one two three four five
a 0 1 2 3 4
b 5 6 7 8 9
ni=n.index.insert(2,'c')
nc=n.columns.insert(5,'six')
n.reindex(index=ni,columns=nc,fill_value=0)
Out[154]:
one two three four five six
a 0 1 2 3 4 0
b 5 6 7 8 9 0
c 0 0 0 0 0 0
.delete(loc) 删除loc位置处的元素
用法与insert相同
n.drop(index,axis=0)
axis=0 默认是行
axis=1 列
数据运算时,倾向将对象看做一个整体,类似行列式,矩阵;对整体进行运算
默认在1轴(即航运算),如果想要进行列运算,要制定axis=0
第三个问题
和其他数据处理第三方库相比,pandas有什么不同&相同之处?
numpy是基础数据类型,常与pandas搭配使用
pandas是扩展的数据类型
numpy更注重数据维度
pandas更注重数据与索引之间的关系和运算
一种观念只能生长在一堆观念之上,学习新事物尤是如此