Pandas基础操作

#创建series对象及打印,连接,删除
import pandas as pd
s1 = pd.Series([1,2,3,4,5,6],dtype = "int32")
#用字典创建series
s2 = pd.Series({'longitude':39,'latitude':116,'temperature':26})
#打印
print(s1)
print("\n")
print(s2)
print("\n")
#连接
s3 = s1.append(s2)
print(s3)
print("\n")
#删除
s4 = s2.drop("temperature")
print(s4)

0 1
1 2
2 3
3 4
4 5
5 6
dtype: int32

longitude 39
latitude 116
temperature 26
dtype: int64

0 1
1 2
2 3
3 4
4 5
5 6
longitude 39
latitude 116
temperature 26
dtype: int64

longitude 39
latitude 116
dtype: int64

#DataFrame的创建与行列访问(表格型的数据结构)
import pandas as pd
df = pd.DataFrame({'col1':[1,2,5,7],'col2':['a','b','c','d']})
df
col1col2
01a
12b
25c
37d
#由列表组成的字典创建DataFrame
import pandas as pd
lista = [1,2,5,7]
listb = ['a','b','c','d']
df = pd.DataFrame({'col1':lista,'col2':listb})
df
col1col2
01a
12b
25c
37d
#修改DataFrame数据
import numpy as np
import pandas as pd
data=pd.DataFrame(np.arange(16).reshape(4,4),index=['BJ','SH','GZ','SZ'],columns=['q','r','s','t'])
data['q']['BJ']=8
data['t']=8
data['s']['SZ']=8
data
qrst
BJ8128
SH4568
GZ89108
SZ121388
#DataFrame对象的行列删除操作示例
dt1=data.drop('SZ',axis=0)      #删除行
dt2=data.drop(['r','s'],axis=1) #删除列
data.drop('SZ',inplace=True)    #删除行
dt1

qrst
BJ8128
SH4568
GZ89108
dt2
qt
BJ88
SH48
GZ88
SZ128
data
qrst
BJ8128
SH4568
GZ89108
#使用Pandas模块求方差
import numpy as np
import pandas as pd
a = np.arange(0,60,5)
a = a.reshape(3,4)
df = pd.DataFrame(a)
print(df)
print('-------------')
print(df.std())

0 1 2 3
0 0 5 10 15
1 20 25 30 35
2 40 45 50 55
-------------
0 20.0
1 20.0
2 20.0
3 20.0
dtype: float64

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值