已解决
Pandas数据操作_Python数据分析与可视化
来自网友在路上 193893提问 提问时间:2023-11-19 09:50:20阅读次数: 93
最佳答案 问答题库938位专家为你答疑解惑
Pandas数据操作
- 排序操作
- 对索引进行排序
- 按行排序
- 按值排序
- 删除操作
- 算数运算
- 去重
- duplicated()
- drop_duplicates()
- 数据重塑
- 层次化索引
- 索引方式
- 内层选取
- 数据重塑
排序操作
对索引进行排序
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 8In[19]: df.drop('a')
Out[19]: oh te ca
c 3 4 5
d 6 7 8In[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.1692392 0.6892713 0.879309
b 1 -0.6991762 0.2604463 -0.321751
c 1 0.8931052 0.757505
d 2 -1.2233443 -0.802812
dtype: float64
索引方式
In[3]:data['b':'d']
Out[3]:
b 1 -0.6991762 0.2604463 -0.321751
c 1 0.8931052 0.757505
d 2 -1.2233443 -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
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"Pandas数据操作_Python数据分析与可视化":http://eshow365.cn/6-39143-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!