import pandas as pd
data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],
'age': [2.5, 3, 0.5, 3.6, 5, 2, 4.5, 5, 7, 3],
'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']}
labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
df = pd.DataFrame(data=data, index=labels)
#(1) 选择df中列标签为animal和age的数据
df.loc[:, ['animal', 'age']]
#(2)选择visuts大于2的行
df[df['visits']>2]
#(3)将f行的age改为1.5
df.loc['f', 'age']=1.5
#(4)计算visits列的数据总和
df['visits'].sum()
创建一个DataFrame(df),用data做数据,labels做行索引
最新推荐文章于 2024-09-14 19:55:48 发布