Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择列以生成直方图_Python_Pandas_Dataframe_Matplotlib - Fatal编程技术网

Python 选择列以生成直方图

Python 选择列以生成直方图,python,pandas,dataframe,matplotlib,Python,Pandas,Dataframe,Matplotlib,我有一个数据集,有28列。我需要选择一列(年龄),并用它做一个直方图 我的问题是如何只选择年龄列 我尝试了多种方法。但它总是选择所有28列并绘制直方图。我如何只选择一列?任何帮助都会很好 您还可以解释如何使用data.loc方法选择列吗 我把它用作: df.loc[df['OS_STATUS'] == "LIVING", " AGE"].value_counts().sort_index().plot(kind ='bar') 但我不知道如何让它工作。任何建议都是好的 这就是我到目前为止所做

我有一个数据集,有28列。我需要选择一列(年龄),并用它做一个直方图

  • 我的问题是如何只选择年龄列

  • 我尝试了多种方法。但它总是选择所有28列并绘制直方图。我如何只选择一列?任何帮助都会很好

    您还可以解释如何使用data.loc方法选择列吗

    我把它用作:

    df.loc[df['OS_STATUS'] == "LIVING", " 
    AGE"].value_counts().sort_index().plot(kind ='bar')
    
    但我不知道如何让它工作。任何建议都是好的

    这就是我到目前为止所做的:

    cereal = pd.read_excel( 'C:\\Users\\f- 
    PC\\Desktop\\class\\CS\\hw1\\Book2.xlsx')
    cereal.plot(kind = 'bar')
    plt.ylabel('frequency')
    plt.xlabel('age')
    plt.title('histogram')
    plt.show()
    
    试试这个例子:

    df = pd.read_csv('http://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv')
    df['Age'].hist()
    
    输出:


    既然您没有提供样本数据(检查)
    df.loc[df['OS_STATUS']=“LIVING”,“AGE”]
    应该生成一个系列,您能证实吗?它可以编译,但我没有得到任何直方图形式的输出。添加
    导入matplotlib.pyplot作为plt
    然后添加
    plt.show()