将字符串传输到数据框架Python

时间:2018-01-21 14:27:20

标签: python python-3.x pandas dataframe

我有一个大字符串,我必须将其转移到数据框中。例如,字符串是:

  

meals_string =“APPETIZERS南方炸鹌鹑   Greens,Huckleberries,Pecans& Blue Cheese 14.00 Park Avenue Cafe   切碎的沙拉山羊奶酪,Nigoise橄榄,腌制白[...]   ENTREES辣根结痂加拿大三文鱼,马铃薯油条,腌制   黄瓜,韭菜香醋27.00炒蘑菇虾   意大利式饺子,烤番茄油醋汁&甜玉米29.50“

meals = meals_string.splitlines()

这给了我var“meal”作为列表,但我被困在如何将字符串转换为3列数据框:Category; Meal_name;价

1 个答案:

答案 0 :(得分:0)

可以构建一个相对简单的字符串解析器,并直接传递给pandas.DataFrame,如:

代码:

def meal_string_parser(meal_string):
    category = ''
    meal = []
    price = 0
    for word in meal_string.split():
        if word:
            try:
                price = float(word)
                yield category, ' '.join(meal), price
                meal = []
            except ValueError:
                # this is not a number, so not a price
                if word.upper() == word and word.isalnum():
                    # found category
                    category = word
                else:
                    meal.append(word)

    if meal:
        yield category, ' '.join(meal), price

测试代码:

meals_string = """
APPETIZERS 
    Southern Fried Quail with Greens,Huckleberries,Pecans & Blue Cheese 14.00
    Park Avenue Cafe Chopped Salad Goat Feta Cheese,Nigoise Olives,Marinated White 13.00 
ENTREES
    Horseradish Crusted Canadian Salmon,Potato Fritters, Marinated Cucumbers,Chive Vinaigrette 27.00
    Sautéed Prawns with Mushroom Tortellini,Grilled Tomato Vinaigrette & Sweet Corn 29.50
"""

import pandas as pd
df = pd.DataFrame(meal_string_parser(meals_string),
                  columns='Category Meal_name Price'.split())
print(df)

结果:

     Category                                          Meal_name  Price
0  APPETIZERS  Southern Fried Quail with Greens,Huckleberries...   14.0
1  APPETIZERS  Park Avenue Cafe Chopped Salad Goat Feta Chees...   13.0
2     ENTREES  Horseradish Crusted Canadian Salmon,Potato Fri...   27.0
3     ENTREES  Sautéed Prawns with Mushroom Tortellini,Grille...   29.5