【发布时间】:2020-01-16 09:06:09
【问题描述】:
我有一个数据框,它与一家商店及其客户的购买情况有关。
我想以某种格式输出数据框中的数据。 数据框由以下列组成:
Customer ID、# of products、List of Products、Class of product。
数据框中的一些条目示例如下:
df = [{Customer ID: 00001, 00002, 00003},
{# of products: 3, 2, 5},
{List of Products: (Milk, Cheese, Bread), (Butter, Steak), (Bread, Apple, Steak, Pasta, Bananas)},
{Class of Product: {[1,2,'D'], [3,3,'G']}, {[1,1,'D'], [2,2,'M']}, {[1,1,'G'], [2,2,'F'],[3,3,'M'], [4,4,'G'], [5,5,'F']}
我希望文本文件输出如下:
00001 # Customer ID
3 # Number of Products
Milk Cheese Bread # List of Products separated using single spacing
D D G # Class corresponding to the products, where D = dairy, G = Gluten, also separated using single spacing
# New line
00002 # Next customer number (Next row of data frame)
2 # number of products
Butter Steak # List of products they purchased separated using single spacing
D M # Class corresponding to the products, where D = Dairy and M = Meat, also separated using single spacing
# New Line
00003 # Next customer number (Next row of data frame)
5 # number of products
Bread Apple Steak Pasta Bananas # List of products separated using single spacing,
G F M G F # Corresponding to the products where F = Fruit, also separated using single spacing
# New Line
整个数据框以此类推。
我不确定如何指定文本文件的具体格式,以及如何确保每个产品的产品类别正确打印。 以客户 00001 为例: [1,2,'D'], [3,3,'G'],确保该类以正确的顺序打印为 D D G,并带有单个间距。
更新:
Customer_ID Num_Items List_of_Products Classes
00001 3 Milk Cheese Bread [[1,2,'D'],[3,3,'G']]
00002 2 Butter Steak [[1,1,'D'],[2,2,'M']]
00003 5 Bread Apple Steak Pasta Bananas [[1,1,'G'], [2,2,'F'], [3,3, 'M'], [4,4,'G'], [5,5,'F']
【问题讨论】:
标签: python pandas dataframe text output