【问题标题】:How create structured DataDrame from multiples arrays and lists?如何从多个数组和列表创建结构化 DataFrame?
【发布时间】:2023-03-31 03:48:02
【问题描述】:

我正在使用 Python3。 我有三个列表,一个是产品经销商的名称,另一个是产品列表,另一个是产品分类,最后,我有两个数组。

每个经销商都提供 15 种产品。

distributors = ['d1', 'd2', 'd3', 'd4', 'd5']
products = ['apple', 'carrot', 'potato', 'avocado', 'pumkie', 'banana', 'kiwi', 'lettuce', 'tomato', 'pees', 'pear', 'berries', 'strawberries', 'blueberries', 'boxes']
tips = ['fruit', 'vegetables', 'random']

actual_prix = np.random.rand(15, 5)
prix_prox_year = np.random.rand(15,5)

数组的结构如下:行是按顺序排列的产品,列是按顺序排列的分销商。 我需要的输出如下:

       Products  Distributor   Actual      Next_year    Type
    0  apple         d1      0.16147847    0.28173206   fruit
    1    ...        ...        ...          ...         fruit
    2  apple         d5        ...          ...         fruit
   ...   ...        ...        ...          ...          ...
   15  boxes         d5        ...          ...         random

这只是一个例子,因为我的数组有这个大小(1010, 33)

有什么想法吗?

【问题讨论】:

  • 您如何知道哪个经销商与哪些产品相关联?

标签: python arrays pandas list dataframe


【解决方案1】:

您可以使用 itertools 中的 product 来创建所有交互,排序对于获取数据模式很重要。对于您需要平铺的数组,以便为​​tips 中的每个元素重复它并分解成一个长数组,以便长度匹配。

我将您的一个数组更改为增加计数,这样很明显发生了什么。

样本数据

import numpy as np

distributors = ['d1', 'd2', 'd3', 'd4', 'd5']
products = ['apple', 'carrot', 'potato', 'avocado', 'pumkie', 'banana', 
            'kiwi', 'lettuce', 'tomato', 'pees', 'pear', 'berries', 'strawberries', 
            'blueberries', 'boxes']
tips = ['fruit', 'vegetables', 'random']

actual_prix = np.arange(15*5).reshape(15,5)
prix_prox_year = np.random.rand(15,5)

from itertools import product
import pandas as pd


df = (pd.DataFrame([*product(products, tips, distributors)],
                   columns=['Products', 'Type', 'Distributor'])
        .assign(Actual = np.tile(actual_prix, len(tips)).ravel(),
                Next_year = np.tile(prix_prox_year, len(tips)).ravel()))

print(df)

    Products        Type Distributor  Actual  Next_year
0      apple       fruit          d1       0   0.391903
1      apple       fruit          d2       1   0.378865
2      apple       fruit          d3       2   0.056134
3      apple       fruit          d4       3   0.623146
4      apple       fruit          d5       4   0.879184
5      apple  vegetables          d1       0   0.391903
6      apple  vegetables          d2       1   0.378865
...
219    boxes  vegetables          d5      74   0.804884
220    boxes      random          d1      70   0.900764
221    boxes      random          d2      71   0.455267
222    boxes      random          d3      72   0.489814
223    boxes      random          d4      73   0.054597
224    boxes      random          d5      74   0.804884

【讨论】:

  • 比我的更好、更完整的答案。干得好。
  • 只有一个问题,我想要的是苹果看起来像水果,盒子像随机,生菜像蔬菜,我的意思是,该行应该将对象分类为这三个类别。我做了一本字典,但我现在不知道该怎么做,因为有我想要的列类型
【解决方案2】:

您可以使用 itertools 和 set 函数来获取唯一的组合,然后将其放入数据框中。

import itertools
store = ["a", "b", "c"]
prods = ['apple', 'banana']

all_combinations = [list(zip(each_permutation, prods)) for each_permutation in itertools.permutations(store, len(prods))]

【讨论】:

    猜你喜欢
    • 2018-12-19
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 2019-03-23
    相关资源
    最近更新 更多