I am trying to use Pandas to create a dataframe from a raw text file. The file includes 3 Categories with items related to each category after the category name. I am able to create a series based on the Category but don't know how to associate each item type to their respective category and create a dataframe out of it. Below is my initial code along with the desired output of the dataframe. Can you please help direct me in the right way to do this?
category = ['Fruits', 'Vegetables', 'Meats']
items='''Fruits
apple
orange
pear
Vegetables
broccoli
squash
carrot
Meats
chicken
beef
lamb'''
Category = pd.Series()
i = 0
for item in items.splitlines():
if item in category:
Category = Category.set_value(i, item)
i += 1
df = pd.DataFrame(Category)
print(df)
Desired DataFrame Output:
Category Item
Fruits apple
orange
pear
Vegetables broccoli
squash
carrot
Meats chicken
beef
lamb