1.创建一个DataFrame(df),用data做数据,labels做行索引。数据如下图
import pandas as pd
data={
'animal':['cat','cat','snake','dog','dog','cat','snake','cat','dog','dog'],
'age':[2.5,3.0,0.5,None,5.0,2.0,4.5,None,7.0,3.0],
'visits':[1,3,2,3,2,3,1,1,2,1],
'priority':['yes','yes','no','yes','no','no','no','yes','no','no']
}
df=pd.DataFrame(data,index=['a','b','c','d','e','f','g','h','i','j'])
print(df)
2. 选择df中列标签为animal和age的数据,并输出。
display(df.loc[:,['animal','age']])
3. 选择行为[3, 4, 8],且列为['animal', 'age']中的数据
print(df.loc[df.index[[3, 4, 8]], ['animal', 'age']])
4. 选择animal为cat,且age小于3的行
df[(df['animal']=='cat')&(df['age']<3)]
5. 将f行的age改为1.5
df.loc['f','age']=1.5
print(df)
6. 计算每种animal的平均age,分组输出。
cat=df[(df['animal']=='cat')]
print("cat平均年龄")
cat[['age']].mean(axis=0)
snake=df[(df['animal']=='snake')]
print("snake平均年龄")
snake[['age']].mean(axis=0)
dog=df[(df['animal']=='dog')]
print("dog平均年龄")
dog[['age']].mean(axis=0)