使用pandas:
import pandas as pd person = { "name":['Google','runoob','Rootroot','Taotao'], "age":[50,40,40,12345] } df=pd.DataFrame(person) print(df) for x in df.index: if df.loc[x,'age'] > 100: df.loc[x,'age'] = 30 print(df)
import pandas as pd person = { "name":['Google','Runoob','runoob','Taoao'], "age" : [50,40,40,12345] } df = pd.DataFrame(person) print(df) df.drop_duplicates(inplace=True) print(df) matplotlib与numpy连动
import matplotlib.pyplot as plt import numpy as np #准备数据 Ypoints=np.array([5,6,8,45,56,12,45,31,2,89]) Ypoints2=np.array([7,8,12,56,68,28,62,45,17,99])*2 #数据放进图表 plt.plot(Ypoints,'o:g') plt.plot(Ypoints,marker='o',ls=':',c='g',linewidth='2') plt.plot(Ypoints2) #添加标题,X轴名字,y轴名字 plt.title("title") plt.xlabel('x-label') plt.ylabel('y-label') plt.grid() #显示 plt.show()
import matplotlib.pyplot as plt import numpy as np Ypoints=np.array([5,6,8,45,56,12,25,45,31,2,89]) plt.subplot(1,2,1) plt.plot(Ypoints,marker='o',ls=':',c='g',linewidth='2') Ypoints2=np.array([7,8,12,56,68,28,62,45,17,99])*2 plt.subplot(1,2,2) plt.plot(Ypoints2) plt.show()
import matplotlib.pyplot as plt import numpy as np x = np.array([1,2,3,4,5,6,7,8]) Y = np.array([1,4,9,16,7,11,23,18]) #指定每一个点的大小 sizes = np.array([20,50,100,200,500,1000,60,90]) #指定每一个点的颜色 colors = np.array(["red","green","black","orange","purple","beige","cyan","magenta"]) #scatter方法将数据导入散点图 plt.scatter(x,Y,s=sizes,c=colors) plt.show()
import matplotlib.pyplot as plt import numpy as np #随机生成五十个数据 x = np.random.rand(50) y = np.random.rand(50) plt.subplot(2,2,1) plt.scatter(x,y,color = 'beige') x2 = np.random.rand(50) y2 = np.random.rand(50) plt.subplot(2,2,4) plt.scatter(x2, y2, color = 'red') plt.show()
import numpy as np import matplotlib.pyplot as plt #准备数据 x = np.array(['baseketball','football','baseball','beautiful']) y = np.array([10,20,20,9]) c = np.array(["#4caf50","red","hotpink","#556b2f"]) #插入数据 plt.bar(x,y,color=c,width=0.4) plt.title('hobby') plt.show()
import numpy as np import matplotlib.pyplot as plt #准备数据 y = np.array([35,25,25,15]) l = ['baseball','tennis','pingpong',"others"] c = np.array(["#4caf50","red","hotpink","#556b2f"]) e = (0.1,0,0,0) #插入数据 plt.pie(y,labels=l,colors=c,autopct='%.1f%%',explode=e) plt.title('hobby') plt.savefig('hobby.png') plt.show()
使用numpy+matplotlib+pandas+seaborn
import numpy as np import pandas as dp import matplotlib.pyplot as plt import seaborn as sns #准备数据 x = np.random.rand(100) y = np.random.rand(100) #matplot画图 plt.scatter(x,y,marker='x') plt.show() #用seaborn画散点图 df=dp.DataFrame({'x':x,'y':y}) sns.jointplot(x='x',y='y',data=df,kind='scatter') plt.show() #折线图数据准备 x = [2021,2011,2012,2013,2014,2015,2016,2017,2018,2019] y = [5,3,6,20,17,16,19,30,32,35] plt.plot(x,y) plt.show() #用seabor画图 df=dp.DataFrame({'x':x,'y':y}) sns.lineplot(x='x',y='y',data=df) plt.show()