
実現したいこと
WEBアンケート調査で価格に関する回答別(4種)に滑らかな曲線を描き、検討に使いたい。
Q1,Q2,Q3,Q4には自由回答の金額(数字データ)が得られる予定。現在はダミーデータで練習中
前提
・pythonで計算し可視化したい。
・下記マクロミル社が提唱しているprice2のような事を自分でもやりたい。
https://www.macromill.com/service/data-analysis/price2/
・ダミーデータ
Price Q1 Q2 Q3 Q4
1000 3000 7000 10000 1500
2000 1500 2900 8000 300
3000 1169 5347 6496 695
4000 927 2712 9574 400
5000 1042 4402 7456 253
6000 1048 5607 7171 105
7000 1155 5319 6747 231
8000 713 4198 7469 271
9000 522 3920 8276 227
10000 796 5989 5973 181
1079 5741 9683 146
541 3959 5089 291
698 3110 7165 185
814 4475 7334 604
1027 5283 9624 169
1101 5568 6817 266
1148 3649 8574 240
1049 2766 7968 113
1154 4289 5254 545
951 5497 7042 440
579 3911 8631 300
941 4542 9875 264
1112 4552 7130 318
680 4624 9846 276
1147 3134 5597 270
1072 3451 7647 727
1064 4825 8290 407
667 4420 5969 434
1025 4901 8115 579
724 4414 5300 207
1123 5237 9626 464
568 2588 8484 519
646 3584 5872 596
738 5752 5767 395
632 5296 8781 464
751 3747 7264 276
915 3576 5882 537
1096 5946 8203 423
1067 3791 7880 476
558 4294 9644 293
791 3698 7567 173
523 4924 6185 709
767 4643 8629 149
1129 5920 5532 384
1137 5275 9268 454
644 4393 8834 229
試したこと
・(滑らかにできないので)1000円から10000円までのbinを作り該当する件数を全体件数で割り、割合を出す。
・Chat gptに相談したが、グラフが未出力だったり数日間問答してもダミーデータの1-2件ともマッチしない。
該当のソースコード *こちらに合わせなくて問題ございません。
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
data = pd.read_csv("test_price1.csv")
bins = [0, 2000, 4000, 6000, 10000]
labels = ['0-2000', '2001-4000', '4001-6000', '6001-10000']
data['Price_Range'] = pd.cut(data['Price'], bins=bins, labels=labels)
total_responses = len(data) # 全体の回答数
data['Percentage_Q1'] = data['Q1'].apply(lambda x: 1 if x >= 2000 else 0).groupby(data['Price_Range']).transform('sum') / total_responses
data['Percentage_Q2'] = data['Q2'].apply(lambda x: 1 if x >= 2000 else 0).groupby(data['Price_Range']).transform('sum') / total_responses
data['Percentage_Q3'] = data['Q3'].apply(lambda x: 1 if x >= 2000 else 0).groupby(data['Price_Range']).transform('sum') / total_responses
data['Percentage_Q4'] = data['Q4'].apply(lambda x: 1 if x >= 2000 else 0).groupby(data['Price_Range']).transform('sum') / total_responses
plt.figure(figsize=(10, 6))
sns.set(style='whitegrid')
sns.lineplot(data=data, x='Price_Range', y='Percentage_Q1', label='Q1', marker='o', color='blue')
sns.lineplot(data=data, x='Price_Range', y='Percentage_Q2', label='Q2', marker='s', color='green')
sns.lineplot(data=data, x='Price_Range', y='Percentage_Q3', label='Q3', marker='^', color='orange')
sns.lineplot(data=data, x='Price_Range', y='Percentage_Q4', label='Q4', marker='D', color='red')
plt.xlabel('Price Range')
plt.ylabel('Percentage of Respondents')
plt.title('Percentage of Respondents by Price Range')
plt.grid(True)
plt.gca().yaxis.set_major_formatter('{:.0%}'.format) # 垂直軸を%表記にする
plt.ylim(0, 1) # 垂直軸の範囲を0%から100%に設定
plt.tight_layout() # レイアウトを調整して重なりを解消
plt.show()

0 コメント