1

2019-3-30
March 30, 2019
In [3]: import numpy as np
import pandas as pd
obj3=pd.Series(['blue','purple','yellow'],index=[0,2,4])
obj3
Out[3]: 0 blue
2 purple
4 yellow
dtype: object
In [5]: obj3.reindex(range(6),method='ffill')
Out[5]: 0 blue
1 blue
2 purple
3 purple
4 yellow
5 yellow
dtype: object
In [7]: obj3.reindex(range(8))
Out[7]: 0 blue
1 NaN
2 purple
3 NaN
4 yellow
5 NaN
6 NaN
7 NaN
dtype: object
In [12]: frame=pd.DataFrame(np.arange(9).reshape((3,3)),index=['a','c','d'],columns=['Ohio','Texas','California'])
frame
Out[12]: Ohio Texas California
a 0 1 2
c 3 4 5
d 6 7 8
1
In [127]: frame2=frame.reindex(['a','b','c','d'])
frame2
Out[127]: d a b c
a NaN NaN NaN NaN
b NaN NaN NaN NaN
c NaN NaN NaN NaN
d NaN NaN NaN NaN
In [17]: frame2=frame.reindex(['a','b','c','d'],method='ffill')
frame2
Out[17]: Ohio Texas California
a 0 1 2
b 0 1 2
c 3 4 5
d 6 7 8
In [20]: frame2=frame.reindex(['a','b','c','d'],fill_value=100)
frame2
Out[20]: Ohio Texas California
a 0 1 2
b 100 100 100
c 3 4 5
d 6 7 8
In [24]: obj=pd.Series(np.arange(5.),index=['a','b','c','d','e'])
obj
Out[24]: a 0.0
b 1.0
c 2.0
d 3.0
e 4.0
dtype: float64
In [26]: new_obj=obj.drop('c')
new_obj
Out[26]: a 0.0
b 1.0
d 3.0
e 4.0
dtype: float64
In [28]: obj
2
Out[28]: a 0.0
b 1.0
c 2.0
d 3.0
e 4.0
dtype: float64
In [30]: obj.drop(['d','c'])
Out[30]: a 0.0
b 1.0
e 4.0
dtype: float64
In [32]: obj.drop(['d','d'])
Out[32]: a 0.0
b 1.0
c 2.0
e 4.0
dtype: float64
In [60]: dframe=pd.DataFrame(np.arange(16).reshape((4,4)),index=['Ohio','Texas','California','New york'],columns=['one','two','three','four'])
dframe
Out[60]: one two three four
Ohio 0 1 2 3
Texas 4 5 6 7
California 8 9 10 11
New york 12 13 14 15
In [36]: dframe.drop(['California','Ohio'])
Out[36]: one two three four
Texas 4 5 6 7
New york 12 13 14 15
In [43]: dframe.drop('two',axis=1)
Out[43]: one three four
Ohio 0 2 3
Texas 4 6 7
California 8 10 11
New york 12 14 15
In [44]: dframe.drop(['two','four'],axis='columns')
Out[44]: one three
Ohio 0 2
Texas 4 6
California 8 10
New york 12 14
3
In [47]: obj=pd.Series(np.arange(4.),index=['a','b','c','d'])
obj
Out[47]: a 0.0
b 1.0
c 2.0
d 3.0
dtype: float64
In [49]: obj=pd.Series(np.arange(5.),index=['a','b','c','d','e'])
obj['b']
Out[49]: 1.0
In [51]: obj[1]
Out[51]: 1.0
In [53]: obj[2:4]
Out[53]: c 2.0
d 3.0
dtype: float64
In [56]: obj[['b','c']]
Out[56]: b 1.0
c 2.0
dtype: float64
In [58]: obj[[1,3]]
Out[58]: b 1.0
d 3.0
dtype: float64
In [61]: obj[obj<2]
Out[61]: a 0.0
b 1.0
dtype: float64
In [89]: data=pd.DataFrame(np.arange(16).reshape((4,4)),index=['Ohio','Texas','California','New york'],columns=['one','two','three','four'])
data
Out[89]: one two three four
Ohio 0 1 2 3
Texas 4 5 6 7
California 8 9 10 11
New york 12 13 14 15
4
In [90]: data['two']
Out[90]: Ohio 1
Texas 5
California 9
New york 13
Name: two, dtype: int32
In [69]: data[['two','one']]
Out[69]: two one
Ohio 1 0
Texas 5 4
California 9 8
New york 13 12
In [70]: data[:2]
Out[70]: one two three four
Ohio 0 1 2 3
Texas 4 5 6 7
In [73]: data[data['three']>5]
Out[73]: one two three four
Texas 4 5 6 7
California 8 9 10 11
New york 12 13 14 15
In [75]: data<5
Out[75]: one two three four
Ohio True True True True
Texas True False False False
California False False False False
New york False False False False
In [80]: data[data<5]==100
data
Out[80]: one two three four
Ohio 100 100 100 100
Texas 100 5 6 7
California 8 9 10 11
New york 12 13 14 15
In [84]: data.loc['California',['two','three']]
Out[84]: two 9
three 10
Name: California, dtype: int32
5
In [85]: data.iloc[2,[3,0,1]]
Out[85]: four 11
one 8
two 9
Name: California, dtype: int32
In [87]: data.iloc[2]
Out[87]: one 8
two 9
three 10
four 11
Name: California, dtype: int32
In [91]: data.iloc[[1,2],[3,0,1]]
Out[91]: four one two
Texas 7 4 5
California 11 8 9
In [93]: frame=pd.DataFrame(np.random.randn(4,3),index=['Ohio','Texas','California','New york'],columns=list('bde'))
frame
Out[93]: b d e
Ohio 0.529956 0.901563 0.823381
Texas -0.098155 0.151220 -0.686077
California -0.338980 -0.427251 0.524724
New york -0.233375 -0.227129 -0.048699
In [95]: f=lambda x:x.max()-x.min()
frame.apply(f)
Out[95]: b 0.868937
d 1.328814
e 1.509458
dtype: float64
In [97]: frame.apply(f,axis=1)
Out[97]: Ohio 0.371607
Texas 0.837297
California 0.951975
New york 0.184676
dtype: float64
In [101]: f=lambda x:x.sum()
frame.apply(f)
6
Out[101]: b -0.140554
d 0.398404
e 0.613330
dtype: float64
In [102]: frame.sum(axis=0)
Out[102]: b -0.140554
d 0.398404
e 0.613330
dtype: float64
In [104]: frame.sum(axis=1)
Out[104]: Ohio 2.254901
Texas -0.633012
California -0.241507
New york -0.509202
dtype: float64
In [116]: obj=pd.Series(range(4),index=['d','a','b','c'])
obj.sort_index()
Out[116]: a 1
b 2
c 3
d 0
dtype: int64
In [109]: frame=pd.DataFrame(np.arange(8).reshape((2,4)),index=['three','one'],columns=['d','a','b','c'])
frame
Out[109]: d a b c
three 0 1 2 3
one 4 5 6 7
In [111]: frame.sort_index()
Out[111]: d a b c
one 4 5 6 7
three 0 1 2 3
In [113]: frame.sort_index(axis=1)
Out[113]: a b c d
three 1 2 3 0
one 5 6 7 4
In [117]: s1=frame.sort_index()
s1.sort_index(axis=1)
7
Out[117]: a b c d
one 5 6 7 4
three 1 2 3 0
In [123]: obj=pd.Series([4,-5,7,-1,20,7,-0.5])
obj.rank()
Out[123]: 0 4.0
1 1.0
2 5.5
3 2.0
4 7.0
5 5.5
6 3.0
dtype: float64
In [125]: obj.rank(method='first')
Out[125]: 0 4.0
1 1.0
2 5.0
3 2.0
4 7.0
5 6.0
6 3.0
dtype: float64
In [126]: obj.rank(method='min')
Out[126]: 0 4.0
1 1.0
2 5.0
3 2.0
4 7.0
5 5.0
6 3.0
dtype: float64
8

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值