【发布时间】:2021-09-04 21:06:49
【问题描述】:
我有四个名为 designName、creatorName、fabric_names 和 data 的列表,data 列表包含多个重复值,我想将其映射到相关的设计名称和创建者名称,例如:
Catching Fireflies thestorysmith FABRIC_PETAL_SIGNATURE_COTTON 1.75 10.58 18.22
Catching Fireflies thestorysmith FABRIC_SATIN 1.75 11.85 19.71
Catching Fireflies thestorysmith FABRIC_COTTON_POPLIN_BRAVA 1.75 11.85 19.71....
Spoonflower Color Map spoonflower_help FABRIC_PETAL_SIGNATURE_COTTON N/A N/A 16.4
Spoonflower Color Map spoonflower_help FABRIC_SATIN N/A N/A 17.74
Spoonflower Color Map spoonflower_help FABRIC_COTTON_POPLIN_BRAVA N/A N/A 17.74...
Night Sky Stars Midnight Blue at_the_cottage FABRIC_PETAL_SIGNATURE_COTTON 1.75 10.58 18.22
Night Sky Stars Midnight Blue at_the_cottage FABRIC_SATIN 1.75 11.85 19.71
Night Sky Stars Midnight Blue at_the_cottage FABRIC_COTTON_POPLIN_BRAVA 1.75 11.85 19.71
数据:
desigName = ['Catching Fireflies', 'Spoonflower Color Map', 'Night Sky Stars Midnight Blue']
creatorName = ['thestorysmith', 'spoonflower_help', 'at_the_cottage']
fabric_names = ['FABRIC_PETAL_SIGNATURE_COTTON', 'FABRIC_SATIN', 'FABRIC_COTTON_POPLIN_BRAVA']
data = [('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),('FABRIC_PETAL_SIGNATURE_COTTON', 1.75, 10.58, 18.22),... ('FABRIC_SATIN', 1.75, 11.85, 19.71),('FABRIC_SATIN', 1.75, 11.85, 19.71),('FABRIC_SATIN', 1.75, 11.85, 19.71),... ('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),('FABRIC_COTTON_POPLIN_BRAVA', 1.75, 11.85, 19.71),...]
我正在尝试找出将这些数据转换为一个有序字典的最佳方法,如下所示。
{('Catching Fireflies', 'thestorysmith'): {'fabric_name_00': 'FABRIC_PETAL_SIGNATURE_COTTON', 'test_swatch_meter_00': 1.75, 'fat_quarter_meter_00': 10.58, 'meter_00': 18.22, 'fabric_name_01': 'FABRIC_SATIN', 'test_swatch_meter_01': 1.75, 'fat_quarter_meter_01': 11.85, 'meter_01': 19.71, 'fabric_name_02': 'FABRIC_COTTON_POPLIN_BRAVA', 'test_swatch_meter_02': 1.75, 'fat_quarter_meter_02': 11.85, 'meter_02': 19.71}}
我试过了:
for fab in fabric_names:
print(fab)
for name, creator in zip(designName, creatorName):
for fab_type in fabric_names:
Design_Name = name
Creator_Name = creator
test_swatch_meter = data[1]
fat_quarter_meter = data[2]
meter = data[3]
if (name, creator) not in items_dict.keys():
items_dict[(name, creator)] = {}
itemCount = len(items_dict[(name, creator)].values()) / 4
items_dict[(name, creator)].update({'fabric_name_%02d' %itemCount: fab_type,
'test_swatch_meter_%02d' %itemCount: test_swatch_meter,
'fat_quarter_meter_%02d' %itemCount: fat_quarter_meter,
'meter_%02d' %itemCount: meter})
df = pd.DataFrame.from_dict(items_dict, orient='index').reset_index(drop=False)
df = df.rename(columns={'level_0':'designName','level_1':'screenName'})
df.to_csv('scraped_data.csv', index=False)
但无法将其格式化为上述格式。
【问题讨论】:
-
欢迎来到Stack Overflow.!如果不查看产生问题的数据和您编写的代码,就很难回答您的问题。请阅读如何提出一个好问题并尝试发布Minimal Reproducible Example,以便我们更好地帮助您。
-
@itprorh66 我用 Minimal Reproducible Example 更新了这个问题。
-
请勿发布代码、数据、错误消息等的图像 - 复制或在问题中键入文本。请保留将图像用于图表或演示渲染错误,这些无法通过文本准确描述的事情。有关更多信息,请参阅元常见问题解答条目Why not upload images of code/errors when asking a question?
-
@itprorh66 我根据我猜的所有要求更新了代码。
-
这看起来很像 xy 问题。我怀疑你的数据结构的用处
标签: python python-3.x pandas list dictionary