获取并绘制Pandas Dataframe 中的唯一值计数

hgc7kmma  于 2022-10-23  发布在  其他
关注(0)|答案(3)|浏览(174)

我有一个 Dataframe df,其中列df.time概述了月份和时间,而df.names则如下所示

time        names
0  2020-01     Martha
1  2020-01     Jeff
2  2020-01     Geronimo
3  2020-02     Mike
4  2020-02     Michelle
...

我想获得每个唯一值的名称总数,并将其绘制在条形图中,其中x上的月份数和y上的值按月份计数

x=df.time.unique()
y=df.groupby('time').size().sort_values(ascending=True)

对于y,我正在查找time中每个唯一日期的名称总数。但是,虽然y确实计算了每个时间值的总数,但顺序不正确,我不知道如何链接它们。

b1zrtrql

b1zrtrql1#

我已经找到了我需要的解决方案

plot_df=df.groupby(['time']).count().sort_values(['names'],ascending=False).rename(columns={'time':'sum of names'})
plot_df=plot_df.sort_values('time').reset_index()
x=plot_df['time'].astype(str)
y=plot_df['names']
plt.bar(x, height=y, width=0.5)

感谢所有发布答案的人,但这实际上是我所期待的结果。

wj8zmpe1

wj8zmpe12#

也许你的想法是这样的?

>>> df.groupby('time')['names'].value_counts(normalize=True).unstack().plot(kind='bar')


小时

sdnqo3pr

sdnqo3pr3#

import matplotlib.pyplot as plt
import seaborn as sns

df2 = df.groupby('time', as_index=False)['names'].value_counts(normalize=True)
sns.barplot(data=df2, x='time', y='proportion', hue='names')
plt.title('Proportion of Unique Values per Day')
plt.show()

输出:


小时
或者,如果你真的想变得花哨:

import plotly.express as px

fig = px.bar(data_frame=df2, x='time', y='proportion', color='names', barmode='group')
fig.show()

输出:

<html>
<head><meta charset="utf-8" /></head>
<body>
    <div>                        <script type="text/javascript">window.PlotlyConfig = {MathJaxConfig: 'local'};</script>
        <script src="https://cdn.plot.ly/plotly-2.14.0.min.js"></script>                <div id="650a21c8-d7dd-4018-9cc4-29554e09fe37" class="plotly-graph-div" style="height:100%; width:100%;"></div>            <script type="text/javascript">                                    window.PLOTLYENV=window.PLOTLYENV || {};                                    if (document.getElementById("650a21c8-d7dd-4018-9cc4-29554e09fe37")) {                    Plotly.newPlot(                        "650a21c8-d7dd-4018-9cc4-29554e09fe37",                        [{"alignmentgroup":"True","hovertemplate":"names=Geronimo<br>time=%{x}<br>proportion=%{y}<extra></extra>","legendgroup":"Geronimo","marker":{"color":"#636efa","pattern":{"shape":""}},"name":"Geronimo","offsetgroup":"Geronimo","orientation":"v","showlegend":true,"textposition":"auto","x":["2020-01"],"xaxis":"x","y":[0.3333333333333333],"yaxis":"y","type":"bar"},{"alignmentgroup":"True","hovertemplate":"names=Jeff<br>time=%{x}<br>proportion=%{y}<extra></extra>","legendgroup":"Jeff","marker":{"color":"#EF553B","pattern":{"shape":""}},"name":"Jeff","offsetgroup":"Jeff","orientation":"v","showlegend":true,"textposition":"auto","x":["2020-01"],"xaxis":"x","y":[0.3333333333333333],"yaxis":"y","type":"bar"},{"alignmentgroup":"True","hovertemplate":"names=Martha<br>time=%{x}<br>proportion=%{y}<extra></extra>","legendgroup":"Martha","marker":{"color":"#00cc96","pattern":{"shape":""}},"name":"Martha","offsetgroup":"Martha","orientation":"v","showlegend":true,"textposition":"auto","x":["2020-01"],"xaxis":"x","y":[0.3333333333333333],"yaxis":"y","type":"bar"},{"alignmentgroup":"True","hovertemplate":"names=Michelle<br>time=%{x}<br>proportion=%{y}<extra></extra>","legendgroup":"Michelle","marker":{"color":"#ab63fa","pattern":{"shape":""}},"name":"Michelle","offsetgroup":"Michelle","orientation":"v","showlegend":true,"textposition":"auto","x":["2020-02"],"xaxis":"x","y":[0.5],"yaxis":"y","type":"bar"},{"alignmentgroup":"True","hovertemplate":"names=Mike<br>time=%{x}<br>proportion=%{y}<extra></extra>","legendgroup":"Mike","marker":{"color":"#FFA15A","pattern":{"shape":""}},"name":"Mike","offsetgroup":"Mike","orientation":"v","showlegend":true,"textposition":"auto","x":["2020-02"],"xaxis":"x","y":[0.5],"yaxis":"y","type":"bar"}],                        {"template":{"data":{"histogram2dcontour":[{"type":"histogram2dcontour","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"choropleth":[{"type":"choropleth","colorbar":{"outlinewidth":0,"ticks":""}}],"histogram2d":[{"type":"histogram2d","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"heatmap":[{"type":"heatmap","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"heatmapgl":[{"type":"heatmapgl","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"contourcarpet":[{"type":"contourcarpet","colorbar":{"outlinewidth":0,"ticks":""}}],"contour":[{"type":"contour","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"surface":[{"type":"surface","colorbar":{"outlinewidth":0,"ticks":""},"colorscale":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]]}],"mesh3d":[{"type":"mesh3d","colorbar":{"outlinewidth":0,"ticks":""}}],"scatter":[{"fillpattern":{"fillmode":"overlay","size":10,"solidity":0.2},"type":"scatter"}],"parcoords":[{"type":"parcoords","line":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterpolargl":[{"type":"scatterpolargl","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"bar":[{"error_x":{"color":"#2a3f5f"},"error_y":{"color":"#2a3f5f"},"marker":{"line":{"color":"#E5ECF6","width":0.5},"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"bar"}],"scattergeo":[{"type":"scattergeo","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterpolar":[{"type":"scatterpolar","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"histogram":[{"marker":{"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"histogram"}],"scattergl":[{"type":"scattergl","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatter3d":[{"type":"scatter3d","line":{"colorbar":{"outlinewidth":0,"ticks":""}},"marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scattermapbox":[{"type":"scattermapbox","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scatterternary":[{"type":"scatterternary","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"scattercarpet":[{"type":"scattercarpet","marker":{"colorbar":{"outlinewidth":0,"ticks":""}}}],"carpet":[{"aaxis":{"endlinecolor":"#2a3f5f","gridcolor":"white","linecolor":"white","minorgridcolor":"white","startlinecolor":"#2a3f5f"},"baxis":{"endlinecolor":"#2a3f5f","gridcolor":"white","linecolor":"white","minorgridcolor":"white","startlinecolor":"#2a3f5f"},"type":"carpet"}],"table":[{"cells":{"fill":{"color":"#EBF0F8"},"line":{"color":"white"}},"header":{"fill":{"color":"#C8D4E3"},"line":{"color":"white"}},"type":"table"}],"barpolar":[{"marker":{"line":{"color":"#E5ECF6","width":0.5},"pattern":{"fillmode":"overlay","size":10,"solidity":0.2}},"type":"barpolar"}],"pie":[{"automargin":true,"type":"pie"}]},"layout":{"autotypenumbers":"strict","colorway":["#636efa","#EF553B","#00cc96","#ab63fa","#FFA15A","#19d3f3","#FF6692","#B6E880","#FF97FF","#FECB52"],"font":{"color":"#2a3f5f"},"hovermode":"closest","hoverlabel":{"align":"left"},"paper_bgcolor":"white","plot_bgcolor":"#E5ECF6","polar":{"bgcolor":"#E5ECF6","angularaxis":{"gridcolor":"white","linecolor":"white","ticks":""},"radialaxis":{"gridcolor":"white","linecolor":"white","ticks":""}},"ternary":{"bgcolor":"#E5ECF6","aaxis":{"gridcolor":"white","linecolor":"white","ticks":""},"baxis":{"gridcolor":"white","linecolor":"white","ticks":""},"caxis":{"gridcolor":"white","linecolor":"white","ticks":""}},"coloraxis":{"colorbar":{"outlinewidth":0,"ticks":""}},"colorscale":{"sequential":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]],"sequentialminus":[[0.0,"#0d0887"],[0.1111111111111111,"#46039f"],[0.2222222222222222,"#7201a8"],[0.3333333333333333,"#9c179e"],[0.4444444444444444,"#bd3786"],[0.5555555555555556,"#d8576b"],[0.6666666666666666,"#ed7953"],[0.7777777777777778,"#fb9f3a"],[0.8888888888888888,"#fdca26"],[1.0,"#f0f921"]],"diverging":[[0,"#8e0152"],[0.1,"#c51b7d"],[0.2,"#de77ae"],[0.3,"#f1b6da"],[0.4,"#fde0ef"],[0.5,"#f7f7f7"],[0.6,"#e6f5d0"],[0.7,"#b8e186"],[0.8,"#7fbc41"],[0.9,"#4d9221"],[1,"#276419"]]},"xaxis":{"gridcolor":"white","linecolor":"white","ticks":"","title":{"standoff":15},"zerolinecolor":"white","automargin":true,"zerolinewidth":2},"yaxis":{"gridcolor":"white","linecolor":"white","ticks":"","title":{"standoff":15},"zerolinecolor":"white","automargin":true,"zerolinewidth":2},"scene":{"xaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2},"yaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2},"zaxis":{"backgroundcolor":"#E5ECF6","gridcolor":"white","linecolor":"white","showbackground":true,"ticks":"","zerolinecolor":"white","gridwidth":2}},"shapedefaults":{"line":{"color":"#2a3f5f"}},"annotationdefaults":{"arrowcolor":"#2a3f5f","arrowhead":0,"arrowwidth":1},"geo":{"bgcolor":"white","landcolor":"#E5ECF6","subunitcolor":"white","showland":true,"showlakes":true,"lakecolor":"white"},"title":{"x":0.05},"mapbox":{"style":"light"}}},"xaxis":{"anchor":"y","domain":[0.0,1.0],"title":{"text":"time"}},"yaxis":{"anchor":"x","domain":[0.0,1.0],"title":{"text":"proportion"}},"legend":{"title":{"text":"names"},"tracegroupgap":0},"margin":{"t":60},"barmode":"group"},                        {"responsive": true}                    )                };                            </script>        </div>
</body>
</html>

相关问题