简体   繁体   English

我如何 plot 多行使用 dataframe 上的值计数

[英]how do i plot multiple lines using value counts on a dataframe

df.groupby('arrival_date_year').market_segment.value_counts()

I used the above code to get the following results:我使用上面的代码得到了以下结果:

arrival_date_year  market_segment
2015               Online TA          6165
                   Groups             6100
                   Offline TA/TO      6079
                   Direct             2314
                   Corporate          1171
                   Complementary       165
                   Undefined             2
2016               Online TA         27661
                   Offline TA/TO     12473
                   Groups             7857
                   Direct             5663
                   Corporate          2562
                   Complementary       364
                   Aviation            127
2017               Online TA         22651
                   Groups             5854
                   Offline TA/TO      5667
                   Direct             4629
                   Corporate          1562
                   Complementary       214
                   Aviation            110

What is the correct syntax to plot multiple lines representing the various market segments with x axis=year and y axis=value count ? plot 表示x axis=yeary axis=value count的各个细分市场的多行的正确语法是什么?

Alternatively, how do I get the data in wide format so that it's easier to compare the counts side by side?或者,如何以宽格式获取数据,以便更容易并排比较计数?

To compare the counts side by side how about并排比较计数怎么样

df.groupby('arrival_date_year').market_segment.value_counts().unstack().reset_index()

eg例如

df
Out[257]: 
    X   Y    Z
0   a  10  100
1   b  20  200
2   a  30  300
3   b  10  400
4   a  20  100
5   b  30  200
6   a  10  300
7   b  20  400
8   a  30  100
9   b  10  200
10  a  20  300
11  b  30  400
df.groupby('X').Y.value_counts().unstack().reset_index()
Out[258]: 
Y  X  10  20  30
0  a   2   2   2
1  b   2   2   2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM