python之pandas库的常用函数

python之pandas库的常用函数

目录
1.求和求平均
mean(axis=0/1,skipna=False) 对列/行求平均值
sum(axis=0/1) 对列/行求和
2.排序
sort_index(ascending,axis) 对列/行索引排序
sort_value(by,axis,ascending) 按某一列(行)的值排序

ps:numpy的其他通用函数同样适用于pandas

代码实现
1.求和求平均

import pandas as pd 
import numpy as np
df = pd.DataFrame({'one':[1,2,3,np.nan],'two':[5,6,7,8]},index=['d','c','b','a'])
#求平均值#
df_mean1 = df.mean(axis = 0)
df_mean2 = df.mean(axis = 1)
#求和#
df_sum1 = df.sum(axis = 0)
df_sum2 = df.sum(axis = 1)

print('df=',df,'\n')
print('df_mean1=',df_mean1,'\n')
print('df_mean2=',df_mean2,'\n')
print('df_sum1=',df_sum1,'\n')
print('df_sum2=',df_sum2,'\n')

df=    one  two
d  1.0    5
c  2.0    6
b  3.0    7
a  NaN    8 

df_mean1= one    2.0
two    6.5
dtype: float64 

df_mean2= d    3.0
c    4.0
b    5.0
a    8.0
dtype: float64 

df_sum1= one     6.0
two    26.0
dtype: float64 

df_sum2= d     6.0
c     8.0
b    10.0
a     8.0
dtype: float64 

2.排序

df = pd.DataFrame({'one':[1,22,3,np.nan],'two':[5,16,27,8]},index=['d','c','b','a'])
#按值排序#
#按 two 列降序排序
df_sort1 = df.sort_values(by = 'two',ascending=False) #ascending=True为正序排序
#按 a 行降序排序
df_sort2 = df.sort_values(by = 'a',ascending=False,axis=1) 
#ps:nan不参与排序,永远放在最后

##按标签排序#
#按列标签降序排序
df_sort3 = df.sort_index(ascending=False,axis=1) 
#按行标签降序排序
df_sort4 = df.sort_index(ascending=False,axis=0) 

print('df=',df,'\n')
print('df_sort1=',df_sort1,'\n')
print('df_sort2=',df_sort2,'\n')
print('df_sort3=',df_sort3,'\n')
print('df_sort4=',df_sort4,'\n')
# print('df_mean2=',df_mean2,'\n')
# print('df_sum1=',df_sum1,'\n')
# print('df_sum2=',df_sum2,'\n')
df=     one  two
d   1.0    5
c  22.0   16
b   3.0   27
a   NaN    8 

df_sort1=     one  two
b   3.0   27
c  22.0   16
a   NaN    8
d   1.0    5 

df_sort2=    two   one
d    5   1.0
c   16  22.0
b   27   3.0
a    8   NaN 

df_sort3=    two   one
d    5   1.0
c   16  22.0
b   27   3.0
a    8   NaN 

df_sort4=     one  two
d   1.0    5
c  22.0   16
b   3.0   27
a   NaN    8 

你可能感兴趣的:(python基础知识,pandas,python,数据分析)