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

时间:2018-12-18 20:13:01

标签: python string dictionary dataframe text

我知道这不可能,但是我仍然想问这个问题:

我的文本文件如下所示:四个带有答案的问题。 enter image description here

将文本文件传输到数据框是否有任何更改? (问题列和行的答案) 这是预期的输出 enter image description here

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""")

上面是文本

1 个答案:

答案 0 :(得分:1)

像这样吗?

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

更新:

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