我必须进行调查,然后将许多学生的答案保存在数据框中。我试图将答案存储在列表中,但不起作用,因为当我尝试将答案保存在数据框中时,我在同一列中得到了具有多个答案的数据框,而不是将许多行作为答案的数量。 这是我的调查
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]`
答案 0 :(得分:1)
我为您提供了可能的解决方案。我创建列表,并在其中添加答案:
sex=[]
country=[]
sport=[]
reason_no_sport=[]
football=[]
basket=[]
swimming=[]
interview='doing'
while interview.upper()=='DOING':
if((input('Can you answer some questions?: ')).upper()=='YES'):
sex.append(input('your sex: '))
country.append(input('where do you come from?: '))
s=input('have you ever play sport?: ')
sport.append(s)
if s.upper() == 'NO':
reason_no_sport.append(input('why didnt you play sport?:'))
football.append(np.nan)
basket.append(np.nan)
swimming.append(np.nan)
else:
reason_no_sport.append(np.nan)
football.append(input('have you ever play football?: '))
basket.append(input('have you ever play basket?: '))
swimming.append(input('have you ever play swimming?: '))
if((input('Do you want to do another interview?: ')).upper()=='YES'):
continue
else:
break
df=pd.DataFrame()
df['sex']=sex
df['country']=country
df['sport']=sport
df['reason_no_sport']=reason_no_sport
df['football']=football
df['basket']=basket
df
输出示例:
Can you answer some questions?: yes
your sex: m
where do you come from?: spain
have you ever play sport?: no
why didnt you play sport?:i don't like it
Do you want to do another interview?: yes
Can you answer some questions?: no
Do you want to do another interview?: yes
Can you answer some questions?: yes
your sex: f
where do you come from?: portugal
have you ever play sport?: yes
have you ever play football?: yes
have you ever play basket?: no
have you ever play swimming?: yes
Do you want to do another interview?: no
sex country sport reason_no_sport football basket swimming
0 m spain no i don't like it NaN NaN NaN
1 f portugal yes NaN yes no yes
您还可以创建一个列表(name
)并询问名称,并将其用作DataFrame
索引:
name=[]
name.append(input('what is your name: '))
df.reindex(name)
答案 1 :(得分:0)
我得到的数据帧在同一列中有多个答案,而不是答案数多行
要解决此问题,就可以做到:
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})
由于它们已经列出,因此您无需使用另一对方括号将它们包装到另一个列表中。
但是要小心:您可能希望在最终数据框中为每个回答调查的人排一行。列表不记得这一点,因为在调查中您不会每次都向所有列表中添加元素(您拥有if else
)。
要解决此问题,您有两种方法。
每次都向每个列表添加一个元素。编辑您的if else
语句,以便在用户不回答问题时,像在@lostCode answer中一样,将None
值添加到相应的列表中。
完全不使用列表。从头开始创建一个空的数据框,并将答案存储为新行。
例如,您可以执行以下操作创建空的数据框:
column_names = ["sex","country","sport",
"reason_no_sport_list","football","basket","swimming"]
df = pd.DataFrame(columns=column_names)
然后,每次向其他人提出调查建议时,请在None
的末尾添加一行df
值:
df = df.append(pd.Series([None]*len(column_names), index=column_names), ignore_index=True)
然后您可以在询问时直接编辑df
的最后一行。例如:
sex = input('your sex: ')
df.loc[len(df)-1, 'sex'] = sex