利用Python进行数据分析——pandas入门
基于NumPy建立的
from pandas importSeries,DataFrame,import pandas as pd
一、两种数据结构
1.Series
类似于Python的字典,有索引和值
创建Series
不指定索引,默认创建0-N
In [54]: obj = Series([1,2,3,4,5])
In [55]: obj
Out[55]:
0 1
1 2
2 3
3 4
4 5
dtype: int64
指定索引
In [56]: obj1 = Series([1,2,3,4,5],index=[‘a’,’b’,’c’,’d’,’e’])
In [57]: obj1
Out[57]:
a 1
b 2
c 3
d 4
e 5
dtype: int64
将Python中的字典转换为Series
In [63]: dic = {‘a’:1,’b’:2,’c’:3}
In [64]: obj2 = Series(dic)
In [65]: obj2
Out[65]:
a 1
b 2
c 3
dtype: int64
对Series进行数组运算(根据布尔型数组进行过滤、标量乘法、应用函数等)依旧会保留索引和值之间的对应关系。
对应index的值找不到就用NAN表示,且在算数运算中会自动补齐数据,不存在用NAN
2.DataFrame
DataFrame是一个表格型的数据结构,既有行索引也有列索引。
创建DataFrame
传进去一个等长列表组成的字典
In [76]: DataFrame(data)
Out[76]:
age name sex
0 23 nadech male
1 25 bob female
指定列的顺序
In [77]: DataFrame(data,columns=[‘sex’,’name’,’age’])
Out[77]:
sex name age
0 male nadech 23
1 female bob 25
嵌套字典创建DataFrame
DataFrame的操作
获取某一列
In [82]: frame[‘age’] /frame.age
Out[82]:
0 23
1 25
Name: age, dtype: int64
赋值
In [86]: frame2
Out[86]:
age sex name grade
0 23 male nadech NaN
1 25 female bob NaN
In [87]: frame2[‘grade’]=12
In [88]: frame2
Out[88]:
age sex name grade
0 23 male nadech 12
1 25 female bob 12
Index对象
In [14]: index = frame.index
In [15]: index
Out[15]: RangeIndex(start=0, stop=3, step=1)
index 对象不可修改
In [16]: index[0]=3
Original: https://blog.csdn.net/weixin_30249465/article/details/113963037
Author: 地球的外星人君
Title: python panda3d从入门_利用Python进行数据分析——pandas入门
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/756582/
转载文章受原作者版权保护。转载请注明原作者出处!