pandas处理interface # import numpy as np import pandas as pd import matplotlib.pyplot as plt #1、生成数据 #2、求和,平均值等 #print(np.arange(0,100,2)) arr1 = np.array([ [1,2,3,4,5], [2,3,45,5,6] ]) print(arr1.shape) print(arr1[0].sum()) ''' pandas 操作是二维的 ''' #1、读取文本 df=pd.read_excel("interface1.xls") #print(df) print("=======df.shape:\n",df.shape) #获取行和列 print("=======df.info():",df.info()) #查看具体的缺失数据 print("=======df.describe():\n",df.describe()) #查看具体的描述信息 print("=======查看前3行数据:\n",df.head(3)) #查看前几行数据 print("=======查看后3行数据:\n",df.tail(3)) #查看后几行数据 #2、查询,按行,按列,按照行和列查询 print("========df['端口名称']:\n",df['端口名称']) print("========df.端口名称\n",df.端口名称) print("========df.loc[0:3,:],前面是行,逗号后面是列:\n",df.loc[0:3,:]) print("=======按照行和列的区域查询\n",df.loc[0:3,"端口名称":"PhyStatus"]) #按照行和列的区域查询 print("=======查出特定的列,特定的行\n",df.loc[[0,3],["端口名称","LinkStatus"]]) #年龄>16的 print("=======年龄>45的:\n",df[df['age']>45]) #3、填充数据 #3.1 年龄填充最小值 age_min = df['age'].min() df['age'] = df['age'].fillna(age_min) #填充函数 fillna print(df) #3.2性别填充众数 mode=df['PhyStatus'].mode() print(mode) df['PhyStatus'] = df['PhyStatus'].fillna(mode) print(df) #4、将年龄按照升序排序后,保存到文本中 df = df.sort_values(by='age', ascending=False) #升序就是true df.to_csv("new.csv",index=False)
pandas
最新推荐文章于 2025-05-23 09:16:47 发布