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

Pandas数据操作

排序操作

对索引进行排序

Series 用 sort_index() 按索引排序,sort_values() 按值排序;

DataFrame 也是用 sort_index() 和 sort_values()。

csharp 复制代码
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

按行排序

csharp 复制代码
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:

csharp 复制代码
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:

csharp 复制代码
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 的一个元素:

csharp 复制代码
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 的行或列:

csharp 复制代码
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 代替。

csharp 复制代码
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 的话,可以传入填充值。

csharp 复制代码
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,表示各行是否是重复行。具体用法如下:

csharp 复制代码
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() 用于去除重复的行数,具体用法如下:

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

数据重塑

层次化索引

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

csharp 复制代码
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

索引方式

csharp 复制代码
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

内层选取

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

数据重塑

将 Series 转化成 DataFrame:

csharp 复制代码
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
相关推荐
言乐67 小时前
HTML视频审核模型
python·django·virtualenv·pygame·tornado
言乐68 小时前
Python根据无法识别搜索词找出可能输入内容模型
开发语言·python·django·virtualenv·pygame
yl45309 小时前
硫酸泄露处理生产商怎么选才够专业
大数据·人工智能·python
笨笨饿9 小时前
140_AI新手村MCP与Skills是干嘛的
开发语言·人工智能·python·stm32·单片机·嵌入式硬件·物联网
for_ever_love__10 小时前
机器学习入门——手写线性回归与梯度下降
人工智能·python·学习·机器学习·线性回归
打工仔折腾 AI10 小时前
从 Demo 到生产级 Agent:8 个关键设计机制与 Python 实现拆解
java·jvm·人工智能·后端·python·langchain·ai agent 实战
I Am a robert girl10 小时前
当传感器学会“说谎“:拆解可靠性门控的稀疏惯性动捕融合
python·姿态估计·传感器融合·惯性动捕·imu传感器·可靠性门控·可穿戴计算
李航198310 小时前
AI定制柜建模,需要详细的建模规范和标准流程
人工智能·python·计算机视觉·ai·ai编程
沙漠之主11 小时前
Python 教学设计资料:从入门到实战的完整课程方案
爬虫·python
ebiobiz12 小时前
极海 APM32 使用 Nimmake 编译指南
c++·python·单片机·嵌入式硬件·mcu