用列计数可视化线路图
我有问题。我有两个列todate
和fromDate
。我想在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
iiuc,您可以通过a dataframe to
sns.sns.sns.lineplot
该列将作为索引的“ x”处理宽形式,并为列处理“ x”:output:
< img src =“ https://i.sstatic.net/avm6i.png” alt =“ lineplot”>
crosstab:
IIUC, you can pass a
pandas.crosstab
DataFrame tosns.lineplot
which will handle a wide form as "x" for the index and "hue" for the columns:output:
crosstab: