酷代码 AI
菜单
全部AI  /  热门  /  AI问答
服务商

使用下列代码创建数据框,完成以下的填空。 import numpy as np import pandas as pd data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'], 'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 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'] 1、使用data为数据,labels为索引,创建数据框df 2、取出 df 的 animal 和 age 列 填空1:df[________]

以下是问题的解答和对应的填空答案: 1、使用 `data` 为数据,`labels` 为索引,创建数据框 `df` 代码:`df = pd.DataFrame(data, index=labels)` 2、取出 `df` 的 `animal` 和 `age` 列 填空1:`df[['animal', 'age']]` 3、取出索引为 `'d'`, `'e'`, `'i'` 的 `animal` 和 `age` 列 填空2:`df.loc[['d', 'e', 'i'], ['animal', 'age']]` 4、取出 `age` 在大于等于 2 岁,小于等于 4 岁的记录 填空3:`df[(df['age'] >= 2) & (df['age'] <= 4)]` 5、取出 `animal` 是 `cat` 或者 `visits` 大于等于 2 的记录 填空4:`df[(df['animal'] == 'cat') | (df['visits'] >= 2)]` 6、查询 `age` 最大的 3 条记录,请用 `nlargest()` 方法 填空5:`df.nlargest(3, 'age')` 7、先按 `age` 降序排列,后按 `visits` 升序排列 填空6:`df.sort_values(by=['age', 'visits'], ascending=[False, True])` [2025-04-14 18:32:16 | AI问答 | 317点数解答]

相关提问