groupByオブジェクトの複数の値(セルあたりのリストに含まれる)を数えたいと思います。私はRecord the respondent’s sex
にグループ分けした後、列7. What do you use the phone for?
のすべての値をカウントしたいgroupbyオブジェクトの複数の値を集計する
| | Record the respondent’s sex | 7. What do you use the phone for? | |---|-----------------------------|---------------------------------------------| | 0 | Male | sending texts;calls;receiving sending texts | | 1 | Female | sending texts;calls;WhatsApp;Facebook | | 2 | Male | sending texts;calls;receiving texts | | 3 | Female | sending texts;calls |
:
は、私は、次のデータフレームを持っています。
セルあたり1つの値しかない場合は、この操作は問題ありません。
| 7. What do you use the phone for? | Record the respondent's sex | count | |-----------------------------------|-----------------------------|-------| | sending texts | Male | 2 | | calls | Male | 2 | | receiving texts | Male | 2 | | sending texts | Female | 2 | | calls | Female | 2 | | WhatsApp | Female | 1 | | Facebook | Female | 1 |
場合にのみ、私は複数の値を扱うこれを取得することができます:
grouped = df.groupby(['Record the respondent’s sex'], sort=True)
question_counts = grouped['2. Are you a teacher, caregiver, or young adult ?'].value_counts(normalize=False, sort=True)
question_data = [
{'2. Are you a teacher, caregiver, or young adult ?': question, 'Record the respondent’s sex': group, 'count': count*100} for
(group, question), count in dict(question_counts).items()]
df_question = pd.DataFrame(question_data)
は私にまさにこのようになりますテーブルを提供します!
value_counts()
複数の値を持つリストでは機能しません。TypeError: unhashable type: 'list'
というエラーがスローされます。質問Counting occurrence of values in a Panda series?は、これをさまざまな方法で処理する方法を示していますが、GroupByオブジェクトで動作させることはできません。列に複数の値を爆発/複製
は確かhttps://stackoverflow.com/questions/12680754/split-explode-pandas-dataframe-string-を参照してください(これについて移動する最も簡単かつ最速の方法のように思えますentry-to-separate-rows)、以下の受け入れられた答えはそれを行うことなく行えることを示しています。 –