【问题标题】:How to pull index of a set of values from a column?如何从列中提取一组值的索引?
【发布时间】:2019-04-18 01:17:03
【问题描述】:

假设我有一个名为 Groceries 的数据集。库存具有商店中食品的条形码及其库存类型。我将它作为数据框加载到 pandas 中,并且我想创建特定库存类型的所有项目的条形码列表,以便我可以使用条形码来识别来自其​​他数据框的信息。

使用 pandas 最有效的方法是什么?

Groceries = {'Stock_type': ['Apple', 'Apple', 'Orange', 'Chicken', 'Orange', 'Chocolate'], 'Bar_Code': [12442, 23534, 53245, 15453, 23453, 13451]}

Groceries_df = pd.DataFrame(data=Groceries)

Groceries_df
   Bar_Code  Stock_type
0     12442       Apple
1     23534       Apple
2     53245      Orange
3     15453     Chicken
4     23453      Orange
5     13451   Chocolate

本质上,这与我想对其进行分析的另一个更大的数据集遇到的问题相同。我需要从一个数据帧中收集唯一标识元数据的列表,以便我可以从其他数据帧中提取这些数据点的值。

【问题讨论】:

  • 你好,你能告诉我们你到底想得到什么吗?我不确定我明白了。
  • ggrelet 我已经重写了这个问题。这是否更清楚?
  • @AngusCampbell 这应该为您提供特定库存类型的条形码列表 Groceries_df[Groceries_df['Stock_type']=='Apple']

标签: python pandas


【解决方案1】:

将数据加载到数据框

import pandas as pd
Groceries = {'Stock_type': ['Apple', 'Apple', 'Orange', 'Chicken', 'Orange', 'Chocolate'], 'Bar_Code': [12442, 23534, 53245, 15453, 23453, 13451]}
Groceries_df = pd.DataFrame(Groceries)

特定库存类型的所有商品的条形码(例如 Apple)

Groceries_df[Groceries_df['Stock_type']=='Apple'] 

条形码的Python列表(将一系列条形码转换为列表)

list(Groceries_df[Groceries_df['Stock_type']=='Apple']['Bar_Code'])

【讨论】:

    猜你喜欢
    • 2018-07-15
    • 2022-01-23
    • 2023-03-31
    • 1970-01-01
    • 2018-09-17
    • 2020-09-17
    • 2016-06-11
    • 1970-01-01
    • 2018-09-25
    相关资源
    最近更新 更多