数据分析之pandas学习笔记一

import numpy as np
import pandas as pd
### series
obj=pd.Series([4,5,6,7])
obj
0 4 1 5 2 6 3 7 dtype: int64
obj.index
RangeIndex(start=0, stop=4, step=1)
obj.values
array([4, 5, 6, 7], dtype=int64)
obj.index.name='number'
obj
number 0 4 1 5 2 6 3 7 dtype: int64 series看成一个定长有序字典,由索引到值的映射,values返回的是array;不同的series可以通过索引关联,未匹配到的以NaN表示 ### dataframe
data=np.empty((3,4))
for i in range(data.shape[0]):
    data[i]=i+1
frame=pd.DataFrame(data,columns=['col_1','col_2','col_3','col_4'],index=['one','two','three'])
frame
col_1col_2col_3col_4
one1.01.01.01.0
two2.02.02.02.0
three3.03.03.03.0
frame.columns
Index([‘col_1’, ‘col_2’, ‘col_3’, ‘col_4’], dtype=’object’)
frame.index
Index([‘one’, ‘two’, ‘three’], dtype=’object’)
frame.index.name='num'
frame
col_1col_2col_3col_4
num
one1.01.01.01.0
two2.02.02.02.0
three3.03.03.03.0
frame['col_1']
one 1.0 two 2.0 three 3.0 Name: col_1, dtype: float64
frame.loc['one']
col_1 1.0 col_2 1.0 col_3 1.0 col_4 1.0 Name: one, dtype: float64
frame['col_5']=0
frame
col_1col_2col_3col_4col_5
num
one1.01.01.01.00
two2.02.02.02.00
three3.03.03.03.00
del frame['col_4']

frame
col_1col_2col_3col_5
num
one1.01.01.00
two2.02.02.00
three3.03.03.00
frame.values
array([[ 1., 1., 1., 0.], [ 2., 2., 2., 0.], [ 3., 3., 3., 0.]]) 1.dataframe表格结构,有行索引和列索引,且面向行列的操作基本是平衡的,一般是二维结构,但可以表示高维数据(层次化索引) 2.通过单个索引返回的是series,并且是相应数据的视图,如需复制需显示copy; 3.dataframe.values 返回与数据对应的n维array
frame.index[0:2]
Index([‘one’, ‘two’], dtype=’object’, name=’num’)
frame.index[2]=3#index对象不可修改
————————————————————————— TypeError Traceback (most recent call last) in () —-> 1 frame.index[2]=3 C:\Users\lz\Anaconda3\lib\site-packages\pandas\indexes\base.py in __setitem__(self, key, value) 1402 1403 def __setitem__(self, key, value): -> 1404 raise TypeError(“Index does not support mutable operations”) 1405 1406 def __getitem__(self, key): TypeError: Index does not support mutable operations
frame
col_1col_2col_3col_5
num
one1.01.01.00
two2.02.02.00
three3.03.03.00
frame.reindex(['two','one','four'],fill_value=0)
col_1col_2col_3col_5
num
two2.02.02.00
one1.01.01.00
four0.00.00.00

reindex返回新的对象(copy参数为True),按照指定索引重新排序,如果未匹配到索引则na(fill_values可以将na填充指定值)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值