python数据处理总结

1.numpy

2.pandas

Series:

s=pd.Series(np.arange(3))
输出:
0    0
1    1
2    2
dtype: int32

替换索引列:

s=pd.Series(np.arange(3),index=['a','b','c'])

DataFrame:

从df中提取两列生成新的DataFrame:

df_new = DataFrame(df, columns = ['May 2018', 'Change.1'])

给某一列赋值:

df_new['Sep 2019'] = np.arange(1,11)

df_new['Sep 2019'] = range(0,10)

df_new['Sep 2019'] = pd.Series(np.arange(2,12))

某一列的两行元素赋值:

df_new['Sep 2019'] = pd.Series([100,200], index=[1,2])

构建DataFrame并且转至

>>> s1 = pd.Series(data['Student'], name = 'Student', index = ['one','two','three'])
>>> s2 = pd.Series(data['Grade'], name = 'Grade', index = ['one','two','three'])
>>> s3 = pd.Series(data['Class'], name = 'Class', index = ['one','two','three'])
>>> df2 = pd.DataFrame([s1,s2,s3])
>>> df2
>>> df2
              one       two     three
Student  XiaoMing  XiaoHong  XiaoWang
Grade         100        90        20
Class         RG1       RG2       RG3
 
# 用列表构建是按行放置的,需要装置一下
 
>>> df2.T
        Student Grade Class
one    XiaoMing   100   RG1
two    XiaoHong    90   RG2
three  XiaoWang    20   RG3

转为excle或者json存储:

df1.to_csv('df1.csv')

df1.to_json('df1.json')

DataFrame的索引,切片

df1['名称']

df1.iloc[1:5,2:4]  #1-4行,2-3列

df1.iloc['a':'c','行名1':'行名2']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值