简体   繁体   English

如何将文本文件转换为数据框(首先是字典)?

[英]How to convert a text file to a dataframe (dictionaries first)?

I know this may not possible but I still want to ask this questions: 我知道这可能无法实现,但我仍然想问以下问题:

My text file looks like below: four questions with answers. 我的文本文件如下所示:四个带有答案的问题。 在此处输入图片说明

Is there any change transferring the text file to a dataframe? 将文本文件传输到数据框是否有任何更改? (questions as columns and answers for rows) This is the expected output (问题列和行的答案)这是预期的输出 在此处输入图片说明

txtfile=StringIO("""1. How do you like this product?
I really don't like this product. It broke after 3-month use
2. Rate your purchasing experience from one to ten?  Will you refer the 
product to your friend?
from 1 to 10, I gave 2
3. What part do you like the most for this product?
The outlook of the product was good but the quality was low
4. Do you have any recommendations that can help us improve?
I don’t think so""")

Above are the text 以上是文字

Something like this? 像这样吗

from io import StringIO
txtfile=StringIO("""1. How do you like this product?
Answer1
2. Rate your purchasing experience from one to ten?  Will you refer the product to your friend?
Answer2
3. What part do you like the most for this product?
Answer3
4. Do you have any recommendations that can help us improve?
Answer4""")

df = pd.read_csv(txtfile,header=None)

df['Answers'] = df[0].str.extract('Answer(\d)')

df = df.bfill()
df = df[~df[0].str.startswith('Answer')]
df.set_index(0).T

Update: 更新:

from io import StringIO
txtfile=StringIO("""1. How do you like this product?
Answer1
2. Rate your purchasing experience from one to ten?  Will you refer the product to your friend?
Answer2
3. What part do you like the most for this product?
Answer3
4. Do you have any recommendations that can help us improve?
Answer4""")

df = pd.read_csv(txtfile,header=None)

ans_dict={'Answer1':"I don't like this product", 'Answer2':'from 1 to 10, I gave 2', 'Answer3':'The outlook of the product was good but quality was low', 'Answer4':"I don't think so Hope it helps"}


df['Answers'] = df[df[0].str.startswith('Answer')]

df['Answers'] = df['Answers'].map(ans_dict)

df = df.bfill()
df = df[~df[0].str.startswith('Answer')]
df.set_index(0).T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM