【问题标题】:Creating a list from a specific column in a dataframe in python从python中数据框中的特定列创建列表
【发布时间】:2018-09-16 07:37:36
【问题描述】:

我有以下数据集地址'https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/Visualization/Online_Retail/Online_Retail.csv'

我使用以下代码导入

online_rt = pd.read_csv('https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/07_Visualization/Online_Retail/Online_Retail.csv', delimiter = ',')
online_rt.head()

这段代码过滤掉一些特定的数据点

Quantity_sum = online_rt.groupby(['Country'])[['Quantity']].sum().sort_values('Quantity', ascending=False)

Top_10 = Quantity_sum.iloc[1:11,:] 

现在我想要将 Country 列转换为单独的列表,将 Quantity 列转换为单独的列表

我用过

Top_10['Country'].tolist() 一栏

Top_10['Quantity'].tolist() 用于另一列

但它一直给我 KeyError :

'Country' 用于第一个列表,KeyError: 'Quantity' 用于第二个 列表

如何为我的数据制作两个单独的列表??

【问题讨论】:

    标签: python python-2.7 pandas


    【解决方案1】:

    groupby 之后,生成的数据框Quantity_sum 只有一列:Quantity,而Country 列现在是索引。

    In [66]: Quantity_sum.head()
    Out[66]:
                    Quantity
    Country
    United Kingdom   4263829
    Netherlands       200128
    EIRE              142637
    Germany           117448
    France            110480
    

    要获取国家/地区列表,您必须通过数据框的index 属性访问它,如下所示:

    In [67]: Top_10.index.tolist()
    Out[67]:
    ['Netherlands',
     'EIRE',
     'Germany',
     'France',
     'Australia',
     'Sweden',
     'Switzerland',
     'Spain',
     'Japan',
     'Belgium']
    

    对于Quantity,你所做的似乎是正确的并且对我有用:

    In [68]: Top_10['Quantity'].tolist()
    Out[68]: [200128, 142637, 117448, 110480, 83653, 35637, 30325, 26824, 25218, 23152]
    

    【讨论】:

    • 干杯!如果对您有帮助,请投票并接受答案:)
    猜你喜欢
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多