import pandas as pd
import numpy as np
# data={
# "name":["唐浩","小王","老王","赵三","李四"],
# "sex":["男","女","男","女","男"],
# "year":[37,22,15,18,33],
# "city":["成都","北京","上海","成都","深圳"]
# }
#Series数据,如果有相同的索引对则进行运算,没有就引入NaN缺省值
# objs1=pd.Series([1,2,3,4,5,6],index=["a","b","c","d","e","f"])
# objs2=pd.Series([1,1,1,1,1,1],index=["a","b","e","f","o","p"])
# print(objs1+objs2) #index相同的就让值+,不一样的就添加,但值为NaN
#DataFrame数据,一样的,相同索引位置才加
# df1=pd.DataFrame(np.arange(0,16).reshape(4,4),index=[1,2,3,4],columns=["a","b","c","d"])
# df2=pd.DataFrame(np.arange(0,16).reshape(4,4),index=[2,8,5,4],columns=["a","e","c","p"])
# print(df1+df2)
#将函数用进来参与处理与运算,这算高级货了》》》
"""
对于一些复杂的运算与处理,我们需要定义函数来处理他,
一般有三种方法:
map函数,装函数套用在Series的每一个元素中
apply函数:将函数套用到DataFrame的行与列上
applymap函数:套用到DataFrame的每一个元素上
"""
#下面我们来看例子,基本操作
# fruit={"name":["苹果","香蕉","桔子","西瓜"],"price":["5.5元","1.5元","3.3元","2.0元"]}
# df_fruit=pd.DataFrame(fruit)
# print(df_fruit)
# def func1(x):
# return x.split("元")[0] #此句功能是将x按元分开,再取第1个元素返回给 func1
#单列数据可以用下面这个来做相应的操作
# df_fruit["price"]=df_fruit["price"].map(func1)
# df_fruit["price"]=df_fruit["price"].map(lambda x:x.split("元")[0]) #用的匿名函数来做
# df_fruit["price"]=df_fruit["price"].map(lambda x:x[:-1]) #用的匿名函数来做
# df_fruit["price"]=df_fruit["price"].map(lambda x:"%.4f"%float(x)) #将数据格式化
# print(df_fruit)
df44=pd.DataFrame(np.arange(0,16).reshape(4,4))
print(df44)
# df45=df44.applymap(lambda x:"%.2f"%x)
df45=df44.applymap(lambda x:x*x) #应用于每个元素
print(df45)
# apply 这里不举例了,行列级操作用它
#这里简单说哈排序:
# 按索引标签排 sort_index(ascending=False) 默认升序
# 按列值排 sort_values(by="列名",ascending=False) ascending=False 降序
原文地址:https://www.cnblogs.com/yiyea/p/11441798.html