用列计数可视化线路图

发布于 2025-01-31 05:03:28 字数 1416 浏览 3 评论 0原文

我有问题。我有两个列todatefromDate。我想在linechart中可视化它们的差异。 X轴应为一个月(例如1,2,3,4等),Y轴应为本月的计数。最后,应该通过what来调用这一点。不幸的是,我没有得到希望的输出。

数据框架

    id  toDate  fromDate
0   1   2021-03-22T18:59:59Z    2021-02-22
1   2   None    2021-03-18
2   3   2021-04-22T18:59:59Z    2021-03-22
3   4   2021-02-15T18:59:59Z    2021-02-10
4   5   2021-09-15T18:59:59Z    2021-09-07
5   6   2020-01-12T18:59:59Z    None
6   7   2022-02-22T18:59:59Z    2022-01-18

代码

import pandas as pd
import seaborn as sns
d = {'id': [1, 2, 3, 4, 5, 6, 7],
     'toDate': ['2021-03-22T18:59:59Z', None, '2021-04-22T18:59:59Z', 
'2021-02-15T18:59:59Z', '2021-09-15T18:59:59Z', '2020-01-12T18:59:59Z', '2022-02-22T18:59:59Z'],
     'fromDate': ['2021-02-22', '2021-03-18', '2021-03-22', 
'2021-02-10', '2021-09-07', None, '2022-01-18']
    }
df = pd.DataFrame(data=d)
display(df)
df_new = pd.DataFrame()
df_new['toDate_month']  = pd.to_datetime(df['toDate'], errors='coerce').dt.month
df_new['fromDate_month']  = pd.to_datetime(df['fromDate'], errors='coerce').dt.month

df_new.melt(var_name='what', value_name='month')

我想要的

sns.lineplot(data=df_new, x="month", y="month".value_counts(), hue="what")

I have a problem. I have two columns toDate and fromDate. I want to visualize the difference of them in a linechart. The x-axis should be the month e.g. (1,2,3,4, etc.) and the y-axis should be the count of the month. And finally this should be hued by what. Unfortunately I did not get the wished output.

Dataframe

    id  toDate  fromDate
0   1   2021-03-22T18:59:59Z    2021-02-22
1   2   None    2021-03-18
2   3   2021-04-22T18:59:59Z    2021-03-22
3   4   2021-02-15T18:59:59Z    2021-02-10
4   5   2021-09-15T18:59:59Z    2021-09-07
5   6   2020-01-12T18:59:59Z    None
6   7   2022-02-22T18:59:59Z    2022-01-18

Code

import pandas as pd
import seaborn as sns
d = {'id': [1, 2, 3, 4, 5, 6, 7],
     'toDate': ['2021-03-22T18:59:59Z', None, '2021-04-22T18:59:59Z', 
'2021-02-15T18:59:59Z', '2021-09-15T18:59:59Z', '2020-01-12T18:59:59Z', '2022-02-22T18:59:59Z'],
     'fromDate': ['2021-02-22', '2021-03-18', '2021-03-22', 
'2021-02-10', '2021-09-07', None, '2022-01-18']
    }
df = pd.DataFrame(data=d)
display(df)
df_new = pd.DataFrame()
df_new['toDate_month']  = pd.to_datetime(df['toDate'], errors='coerce').dt.month
df_new['fromDate_month']  = pd.to_datetime(df['fromDate'], errors='coerce').dt.month

df_new.melt(var_name='what', value_name='month')

What I want

sns.lineplot(data=df_new, x="month", y="month".value_counts(), hue="what")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知所踪 2025-02-07 05:03:28

iiuc,您可以通过a dataframe to sns.sns.sns.lineplot 该列将作为索引的“ x”处理宽形式,并为列处理“ x”:

sns.lineplot(data=pd.crosstab(df_new['month'], df_new['what']))

output:

< img src =“ https://i.sstatic.net/avm6i.png” alt =“ lineplot”>

crosstab:

what   fromDate_month  toDate_month
month                              
1.0                 1             1
2.0                 2             2
3.0                 2             1
4.0                 0             1
9.0                 1             1

IIUC, you can pass a pandas.crosstab DataFrame to sns.lineplot which will handle a wide form as "x" for the index and "hue" for the columns:

sns.lineplot(data=pd.crosstab(df_new['month'], df_new['what']))

output:

lineplot

crosstab:

what   fromDate_month  toDate_month
month                              
1.0                 1             1
2.0                 2             2
3.0                 2             1
4.0                 0             1
9.0                 1             1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文