擅长:python、mysql、java
<p>您可以按“web*”列分组并计算“age”列的平均值。在</p>
<p>您也可以绘制条形图(颜色可以在子图中定义)。我不确定饼图在这种情况下是否有意义。在</p>
<p>我试着用你的<a href="https://jumpshare.com/v/s2WRGzoJHXkrlt3S27Ak" rel="nofollow noreferrer">data</a>,只取以“web”开头的列。值比“1”和“2”多,所以我假设您只想分析用户和非用户,而不分析其他内容。只要您知道要绘制的值,就可以用相同的方法在图表中更改值或添加其他值。在</p>
<pre><code>df = df.filter(regex=('web|age'),axis=1)
userNr = '1'
nonUserNr = '2'
users = list()
nonUsers = list()
labels = [x for x in df.columns.tolist() if 'web' in x]
for col in labels:
users.append(df.loc[:,['age',col]].groupby(col).mean().loc[userNr][0])
nonUsers.append(df.loc[:,['age',col]].groupby(col).mean().loc[nonUserNr][0])
from matplotlib import pyplot as plt
x = np.arange(1, len(labels)+1)
ax = plt.subplot(111)
ax.bar(x-0.1, users, width=0.2,color='g')
ax.bar(x+0.1,nonUsers, width=0.2,color='r')
plt.xticks(x, labels)
plt.legend(['users','non-users'])
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/fYP8i.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fYP8i.jpg" alt="enter image description here"/></a></p>