8.1创建DataFrame
import pandas as pd
dic1={'name':['Tom','Lily','Cindy','Petter'],'no':['001','002','003','004'],'age':[16,16,15,16],'gender':['m','f','f','m']}
df1=pd.DataFrame(dic1)
print('显示该数据结构类型',type(df1))
df1.index.name='id'
df1.to_csv('students.csv')
df1
8.2 DataFrame查询操作
column=df1.no
row=df1.loc[3] #提出指标为3的那一行
print('\n列表数据索引\n',column,
'\n行数据索引\n',row)
8.3 DataFrame增加操作
print('修改前:\n',df1)
df2=df1.append([{'name':'Stack','no':'005','age':15,'gender':'m'}],ignore_index=True) #只能增加一个序列,因此有[]
print('增加行:\n',df2)
df2['new_Col']=[1,2,3,4,5]
print('增加列:\n',df2)
8.4 DataFrame删除操作
df4=pd.DataFrame({'adress':['school','home','school','home']})
df5=pd.concat([df2,df4],axis=1)
print('合并前的df1\n',df2)
print('合并前的df4\n',df4)
print('合并后的df5\n',df5)
8.5 DataFrame修改操作
df4=pd.DataFrame({'adress':['school','home','school','home']})
df5=pd.concat([df1,df4],axis=1) #加一列(按列加)
print('合并前的df1\n',df1)
print('合并前的df4\n',df4)
print('合并后的df5\n',df5)
8.6 DataFrame特殊操作
将索引列变成时间
i1=pd.date_range('2019/9/21',periods=4,freq='7D')
df10=pd.read_csv('students.csv')
df10.index=i1
df10
8.7 DataFrame转换为其它数据结构
print('DataFrame换ndarray\n',df10.values,
"\nDataFrame转Series\n",df10['gender'])