【问题标题】:Acquire the data from a row in a Pandas从 Pandas 中的一行获取数据
【发布时间】:2017-01-21 17:35:39
【问题描述】:

教授的指示: 1. 使用 World Atlas 数据中按大洲划分的国家列表,将 countries.csv 文件加载到 pandas DataFrame 中,并将该数据集命名为国家。 2. 使用 Gapminder 上可用的数据,将人均收入(GDP/人均,PPP$ 通胀调整后)作为 pandas DataFrame 加载,并将该数据集命名为收入。 3. 将数据集转换为以年为行,以国家为列。加载时显示此数据集的头部。 4. 以图形方式显示世界各国在任何给定年份(例如 2000 年)的人均收入分布。什么样的情节最好?

在下面的代码中,我完成了其中一些任务,但我很难理解如何从 DataFrame 行获取数据。我希望能够从一行中获取数据然后绘制它。这似乎是一个微不足道的概念,但我已经研究了一段时间,需要帮助。

%matplotlib inline 
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
countries = pd.read_csv('2014_data/countries.csv')
countries.head(n=3)
income = pd.read_excel('indicator gapminder gdp_per_capita_ppp.xlsx')
income = income.T

def graph_per_year(year):
     stryear = str(year)
     dfList = income[stryear].tolist()
graph_per_year(1801)

【问题讨论】:

标签: python pandas matplotlib dataframe data-science


【解决方案1】:

要回答您的第一个问题,最好使用带有年份部门的条形图。您必须将国家放在 y 轴上,将人均收入放在 y 轴上。还有一个下拉菜单,可以选择图表将更改的特定年份。

【讨论】:

    【解决方案2】:

    Pandas 使用三种类型的索引。

    如果您希望使用整数索引,则需要使用 .iloc

    df_1
    Out[5]: 
              consId  fan-cnt
    0  1155696024483     34.0
    1  1155699007557     34.0
    2  1155694005571     34.0
    3  1155691016680     12.0
    4  1155697016945     34.0
    
    df_1.iloc[1,:] #go to the row with index 1 and select all the columns
    Out[8]: 
    consId     1.155699e+12
    fan-cnt    3.400000e+01
    Name: 1, dtype: float64
    

    要转到特定的单元格,您可以使用以下几行的内容,

    df_1.iloc[1][1]
    Out[9]: 34.0
    

    您需要通过documentation 进行其他类型的索引,即.ix.loc,正如sohier-dane 所建议的那样。

    【讨论】:

      猜你喜欢
      • 2022-06-22
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多