这些数据之前是作为.txt
文件提供的。我将其转换为.csv
格式并尝试将其排序为想要的形式,但失败了。我正在尝试找到转换此数据结构的方法(如下所示):
bakeryA
77300 Baker Street
bun: [10,20,30,10]
donut: [20,10,40,0]
bread: [0,10,15,10]
bakery B
78100 Cerabut St
data not available
bakery C
80300 Sulkeh St
bun: [29,50,20,30]
donut: [10,10,30,10]
bread: [10,15,10,20]
进入这个数据框:
姓名 | 地址 | 类型 | 盐 | 糖 | 水 | 面粉 |
---|---|---|---|---|---|---|
面包店A | 贝克街 77300 号 | 好的 | 10 | 20 | 30 | 10 |
面包店A | 贝克街 77300 号 | 甜甜圈 | 20 | 10 | 40 | 0 |
面包店A | 贝克街 77300 号 | 面包 | 0 | 10 | 15 | 10 |
面包店 B | 78100 Cerabut 街 | 在 | 在 | 在 | 在 | 在 |
面包店 C | 80300 苏尔凯街 | 好的 | 29 | 50 | 20 | 30 |
面包店 C | 80300 苏尔凯街 | 甜甜圈 | 10 | 10 | 30 | 10 |
面包店 C | 80300 苏尔凯街 | 面包 | 10 | 15 | 10 | 20 |
谢谢!
这与 Pandas 关系不大,更多与将非结构化源解析为结构化数据有关。尝试这个:
from ast import literal_eval
from enum import IntEnum
class LineType(IntEnum):
BakeryName = 1
Address = 2
Ingredients = 3
data = []
with open('data.txt') as fp:
line_type = LineType.BakeryName
for line in fp:
line = line.strip()
if line_type == LineType.BakeryName:
name = line # the current line contains the Bakery Name
line_type = LineType.Address # the next line is the Bakery Address
elif line_type == LineType.Address:
address = line # the current line contains the Bakery Address
line_type = LineType.Ingredients # the next line contains the Ingredients
elif line_type == LineType.Ingredients and line == 'data not available':
data.append({
'Name': name,
'Address': address
}) # no Ingredients info available
line_type = LineType.BakeryName # next line is Bakery Name
elif line_type == LineType.Ingredients:
# if the line does not follow the ingredient's format, we
# overstepped into the Bakery Name line. Then the next line
# is Bakery Address
try:
bakery_type, ingredients = line.split(':')
ingredients = literal_eval(ingredients.strip())
data.append({
'Name': name,
'Address': address,
'type': bakery_type,
'salt': ingredients[0],
'sugar': ingredients[1],
'water': ingredients[2],
'flour': ingredients[3],
})
except:
name = line
line_type = LineType.Address
df = pd.DataFrame(data)
这假设您的数据文件采用所示格式。轻微的偏差(例如,空白线)就会将其抛之脑后。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句