【发布时间】:2020-01-01 15:47:34
【问题描述】:
我必须进行一项调查,然后将许多学生的答案保存在数据框中。我尝试将答案存储在列表中但不起作用,因为当我尝试将答案保存在数据框中时,我得到的数据框在同一列中有多个答案,而不是作为答案数的多行。 这是我的调查
sex=input('your sex: ')
sex_list.append(sex)
country=input('where do you come from?: ')
country_list.append(country)
sport=input('have you ever play sport?: ')
sport_list.append(sport)
if sport=='no':
reason_no_sport=input('why didnt you play sport?:')
reason_no_sport_list.append(reason_no_sport)
else:
reason_no_sport=np.nan
reason_no_sport_list.append(reason_no_sport)
football=input('have you ever play football?: ')
football_list.append(football)
basket=input('have you ever play basket?: ')
basket_list.append(basket)
swimming=input('have you ever play swimming?: ')
swimming_list.append(swimming)
这是列表
sex_list=[]
country_list=[]
sport_list=[]
reason_no_sport_list=[]
football_list=[]
basket_list=[]
swimming_list=[]
这是数据框
df = pd.DataFrame({"sex": [sex_list],
"country": [country_list],
"sport":[sport_list],
"why didnt you play sport?": [reason_no_sport_list],
"football":[football_list],
"basket":[basket_list],
"swimming":[swimming_list]})
结果是这样的
sex=`[male, female]`
country= `[usa, england]`
【问题讨论】:
-
您好!您应该添加代码的相关部分,即您尝试将列表转换为数据框的位置。请edit您的帖子添加它。
标签: python pandas store survey