pandas数据对其排序统计计算
加粗样式
前言
提示:以下是本篇文章正文内容,下面案例可供参考
一、索引对象
 
在这里插入代码片
输出索引值
import numpy as np
import pandas as pd
c=np.random.randint(1,10,(5,5))
frame=pd.DataFrame(c,columns=list("abcde"))
print(frame.index)
one=frame.index
print(one)
结果为:
RangeIndex(start=0, stop=5, step=1)
RangeIndex(start=0, stop=5, step=1)
print(frame.columns)
Index(['a', 'b', 'c'], dtype='object')
二、索引重置
 
在这里插入代码片
import numpy as np
import pandas as pd
s=pd.Series([0,1,2,3,4],index=['a','b','c','d'])
print(s.reindex(['a','b','c','d','e'],fill_value=5))fill_value=5 填充缺少的值

在这里插入代码片
import numpy as np
import pandas as pd
print(s.reindex(['a','b','e','f'],method='ffill'))
a 0
b 1
e 3
f 4
dtype: int64
print(s.reindex(['a','b','e','f'],method='bfill'))
a 0
b 1
e 4
f 4
dtype: int64
三、Series索引操作
切片
在这里插入代码片
import numpy as np
import pandas as pd
ser_obj=pd.Series([1,2,3,4],index=['a','b','c','d'])
print(ser_obj)
print(ser_obj[0:2])
print(ser_obj['c':'b'])
结果为:
a 1
b 2
c 3
d 4
dtype: int64
a 1
b 2
dtype: int64
c 3
d 4
dtype: int64
在这里插入代码片
import numpy as np
import pandas as pd
ser_obj=pd.Series([1,2,3,4],index=['a','b','c','d'])
print(ser_obj[2])
print(ser_obj['b'])
结果为:
3
ser_obj=pd.Series([1,2,3,4],index=['a','b','c','d'])
print(ser_obj[[0,2,3]])
a 1
c 3
d 4
dtype: int64
b=[True,True,False,False]
print(ser_obj[b])
a 1
b 2
dtype: int64
print(frame['a'])
0 1
1 4
2 7
Name: a, dtype: int64
 
在这里插入代码片
import numpy as np
import pandas as pd
frame=pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=['a','b','c'])
print(frame.loc[:]['a'])
print(frame.loc[:,['a']])
结果为:
0 1
1 4
2 7
Name: a, dtype: int64
a
0 1
1 4
2 7
print(frame.iloc[0:2])
print(frame.iloc[0:2,:])
结果为:
a b c
0 1 2 3
1 4 5 6
a b c
0 1 2 3
1 4 5 6
二、算数运算与数据对齐
1.填充缺失值
在这里插入代码片
import pandas as pd
import numpy as np
obj_one=pd.Series([10,11,12],index=[0,1,2])
obj_two=pd.Series([20,21,22,23,24],index=[0,1,2,3,4])
result1=obj_one+obj_two
print(result1)
result=obj_two.add(obj_one,fill_value=0)
print(result)
结果为:
0 30.0
1 32.0
2 34.0
3 NaN
4 NaN
dtype: float64
0 30.0
1 32.0
2 34.0
3 23.0
4 24.0
dtype: float64
2.按索引排序

在这里插入代码片
import numpy as np
import pandas as pd
a1=pd.Series([1,2,3,4],index=[9,3,4,1])
print(a1.sort_index())
print(a1.sort_index(ascending=False))
结果为:
1 4
3 2
4 3
9 1
dtype: int64
9 1
4 3
3 2
1 4
dtype: int64
```python
在这里插入代码片
import numpy as np
import pandas as pd
a2=pd.DataFrame([[1,2,3],[4,9,6],[7,8,9]],index=[6,4,9],columns=['a','b','c'])
print(a2.sort_values(by='b',ascending=False))
 
|