我知道这不可能,但是我仍然想问这个问题:
将文本文件传输到数据框是否有任何更改? (问题列和行的答案) 这是预期的输出
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""")
上面是文本
答案 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