Pandas数据操作_Python数据分析与可视化

排序操作

对索引进行排序

Series 用 sort_index() 按索引排序,sort_values() 按值排序;
DataFrame 也是用 sort_index() 和 sort_values()。

In[73]: obj = Series(range(4), index=['d','a','b','c'])
In[74]: obj.sort_index()  
Out[74]: 
a    1
b    2
c    3
d    0
dtype: int64
In[78]: frame = DataFrame(np.arange(8).reshape((2,4)),index=['three', 'one'],columns=['d','a','b','c'])
In[79]: frame
Out[79]: 
       d  a  b  c
three  0  1  2  3
one    4  5  6  7
In[86]: frame.sort_index()
Out[86]: 
       d  a  b  c
one    4  5  6  7
three  0  1  2  3

按行排序

In[89]: frame.sort_index(axis=1, ascending=False)
Out[89]: 
       d  c  b  a
three  0  3  2  1
one    4  7  6  5

按值排序

Series:

In[92]: obj = Series([4, 7, -3, 2])
In[94]: obj.sort_values()
Out[94]: 
2   -3
3    2
0    4
1    7
dtype: int64

DataFrame:

In[95]: frame = DataFrame({'b':[4, 7, -3, 2], 'a':[0, 1, 0, 1]})
In[97]: frame.sort_values(by='b')  #DataFrame必须传一个by参数表示要排序的列
Out[97]: 
   a  b
2  0 -3
3  1  2
0  0  4
1  1  7

删除操作

删除指定轴上的项
即删除 Series 的元素或 DataFrame 的某一行(列)的意思,我们可以通过对象的 drop(labels, axis=0) 方法实现此功能。

删除 Series 的一个元素:

In[11]: ser = Series([4.5,7.2,-5.3,3.6], index=['d','b','a','c'])
In[13]: ser.drop('c')
Out[13]: 
d    4.5
b    7.2
a   -5.3
dtype: float64

删除 DataFrame 的行或列:

In[17]: df = DataFrame(np.arange(9).reshape(3,3), index=['a','c','d'], columns=['oh','te','ca'])
In[18]: df
Out[18]: 
   oh  te  ca
a   0   1   2
c   3   4   5
d   6   7   8

In[19]: df.drop('a')
Out[19]: 
   oh  te  ca
c   3   4   5
d   6   7   8

In[20]: df.drop(['oh','te'],axis=1)
Out[20]: 
   ca
a   2
c   5
d   8

需要注意的是 drop() 返回的是一个新对象,原对象不会被改变。

算数运算

DataFrame 中的算术运算是 df 中对应位置的元素的算术运算,如果没有共同的元素,则用 NaN 代替。

In[5]: df1 = DataFrame(np.arange(12.).reshape((3,4)),columns=list('abcd'))
In[6]: df2 = DataFrame(np.arange(20.).reshape((4,5)),columns=list('abcde'))
In[9]: df1+df2
Out[9]: 
    a   b   c   d   e
0   0   2   4   6 NaN
1   9  11  13  15 NaN
2  18  20  22  24 NaN
3 NaN NaN NaN NaN NaN

此外,如果我们想设置默认的其他填充值,而非 NaN 的话,可以传入填充值。

In[11]: df1.add(df2, fill_value=0)
Out[11]: 
    a   b   c   d   e
0   0   2   4   6   4
1   9  11  13  15   9
2  18  20  22  24  14
3  15  16  17  18  19

去重

duplicated()

DataFrame 的 duplicated 方法返回一个布尔型 Series,表示各行是否是重复行。具体用法如下:

In[1]: df = DataFrame({'k1':['one']*3 + ['two']*4, 'k2':[1,1,2,3,3,4,4]})
In[2]: df
Out[2]: 
    k1  k2
0  one   1
1  one   1
2  one   2
3  two   3
4  two   3
5  two   4
6  two   4
In[3]: df.duplicated()
Out[3]: 
0    False
1     True
2    False
3    False
4     True
5    False
6     True
dtype: bool

drop_duplicates()

drop_duplicates() 用于去除重复的行数,具体用法如下:

In[4]: df.drop_duplicates()
Out[4]: 
    k1  k2
0  one   1
2  one   2
3  two   3
5  two   4

数据重塑

层次化索引

层次化索引(hierarchical indexing)是 pandas 的一项重要功能,它使我们能在一个轴上拥有多个(两个以上)索引级别。请看以下例子:

In[1]:data = Series(np.random.randn(10), index = [['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'd', 'd' ],[1,2,3,1,2,3,1,2,2,3]])
In[2]:data
Out[2]:
a  1    0.169239
   2    0.689271
   3    0.879309
b  1   -0.699176
   2    0.260446
   3   -0.321751
c  1    0.893105
   2    0.757505
d  2   -1.223344
   3   -0.802812
dtype: float64

索引方式

In[3]:data['b':'d']
Out[3]:
b  1   -0.699176
   2    0.260446
   3   -0.321751
c  1    0.893105
   2    0.757505
d  2   -1.223344
   3   -0.802812
dtype: float64

内层选取

In[4]:data[:, 2]
Out[4]:
a    0.689271
b    0.260446
c    0.757505
d   -1.223344
dtype: float64

数据重塑

将 Series 转化成 DataFrame:

in[5]:data.unstack()
Out[5]:
1                    2            3
a    0.169239    0.689271    0.879309
b    -0.699176   0.260446  -0.321751
c    0.893105    0.757505    NaN
d    NaN        -1.223344   -0.802812
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿松爱睡觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值