从Python熊猫访问数据

时间:2020-09-23 14:52:11

标签: python pandas

以下是熊猫的基本分配问题。

创建一个名为heights_A的系列,其值分别为176.2、158.4、167.6, 156.2和161.4。这些值代表5名A级学生的身高。

将每个学生标记为s1,s2,s3,s4和s5。

创建另一个名为weights_A的系列,其值分别为85.1、90.2、76.8, 80.4和78.9。这些值代表5名A类学生的权重。

将每个学生标记为s1,s2,s3,s4和s5。

创建一个名为df_A的数据框,其中包含 五个学生s1,s2,s3,s4和s5。

分别将列标记为Student_height和Student_weight。

选择与学生身高相关的df_A列,并将其存储在 可变高度。注意:在内部指定所需的列名称 方括号。

打印高度可变的对象的类型。

到目前为止,尝试使用以下代码,但是检查器似乎认为height数据类型不正确。 不确定这在哪里不正确。

import pandas as pd
import numpy as np



heights_A =  pd.Series([176.2, 158.4, 167.6, 156.2, 161.4])

heights_A.index = ['s1', 's2', 's3', 's4','s5']


weights_A = pd.Series([85.1, 90.2, 76.8, 80.4 , 78.9])

weights_A.index = ['s1', 's2', 's3', 's4','s5']



df_A = pd.DataFrame()

df_A['Student_height'] = heights_A

df_A['Student_weight'] = weights_A

height = df_A[['Student_height']]
height.dtypes

不确定此高度数据类型零件有何不正确之处

1 个答案:

答案 0 :(得分:0)

只需尝试:

height = df_A['Student_height']
height.dtypes

当您使用双方括号时,heights变量为df,当您仅使用单方括号时,将返回系列。好像您的问题要返回一个系列。