【问题标题】:how to store answers of a survey in a python data frame如何将调查的答案存储在 python 数据框中
【发布时间】: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


【解决方案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)

【讨论】:

    【解决方案2】:

    我得到的数据框在同一列中有多个答案,而不是作为答案数的多行

    要解决这个问题,这样做就足够了:

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

    要解决此问题,您有两种方法。

    1. 每次向每个列表添加一个元素。编辑您的if else 语句,以便当用户没有回答问题时,将None 值添加到相应列表中,如@lostCode answer 中所做的那样。

    2. 根本不要使用列表。从头开始创建一个空数据框,并将答案作为新行存储在其中。
      例如,您可以创建空数据框:

      column_names = ["sex","country","sport",
            "reason_no_sport_list","football","basket","swimming"]
      
      df = pd.DataFrame(columns=column_names)
      

      然后,每次您向其他人提出您的调查时,在您的 df 末尾添加一行 None 值:

      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
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      相关资源
      最近更新 更多