第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

具有矩陣圖和分組熊貓數(shù)據(jù)幀的堆棧圖

具有矩陣圖和分組熊貓數(shù)據(jù)幀的堆棧圖

瀟瀟雨雨 2022-09-13 19:18:09
我正在使用的數(shù)據(jù)是對(duì)話消息日志。我有一個(gè)熊貓數(shù)據(jù)幀,以日期戳作為索引,還有兩列;一個(gè)用于“發(fā)件人”,一個(gè)用于“消息”。我只是試圖繪制一個(gè)隨時(shí)間變化的消息堆棧圖。我實(shí)際上并不需要消息的內(nèi)容,因此我按如下方式清理了數(shù)據(jù):dfgrouped = df.groupby(["sender"])dfgrouped[["sender"]].resample("D").count()這將提供按對(duì)話中的每個(gè)發(fā)件人分組的數(shù)據(jù)幀,其中 DateTime 作為索引,并在給定日期發(fā)送的消息數(shù)。dfgrouped[["sender"]].get_group("Joe Bloggs").resample("D").count()...將給出一個(gè)只有一個(gè)用戶的數(shù)據(jù)幀,并且他們的消息每天計(jì)數(shù)。我想知道如何使用 matplotlib 來(lái)繪制一個(gè)堆棧圖,其中每個(gè)“發(fā)送方”是一條不同的線。我也無(wú)法通過(guò)ax.stackplot(dfgrouped[["sender"]].resample("D").count())或通過(guò)循環(huán):for sender in df["sender"].unique():     axs[i].stackplot(dfgrouped[["sender"]].get_group(sender).resample("D").count()
查看完整描述

1 回答

?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊

您可以使用熊貓自己的堆棧圖函數(shù),df.plot.area()。這是 Matplotlib 函數(shù)的包裝器,用作數(shù)據(jù)幀上的方法。您只需要將數(shù)據(jù)保持在正確的形狀。通過(guò)您的分組和計(jì)數(shù)操作,您幾乎就在那里:

import pandas as pd


df = pd.DataFrame({'sender': [

    'Person 2', 'Person 1', 'Person 2', 'Person 1', 'Person 2', 'Person 1', 'Person 2', 

    'Person 1', 'Person 1', 'Person 2', 'Person 1', 'Person 2', 'Person 1', 'Person 2', 

    'Person 2', 'Person 2', 'Person 2', 'Person 1', 'Person 2', 'Person 1', 'Person 2', 

    'Person 2', 'Person 1', 'Person 2', 'Person 2', 'Person 1', 'Person 2', 'Person 2', 

    'Person 1', 'Person 2', 'Person 1', 'Person 2'], 

    'message': [

    'Hello', 'Hi there', "How's things", 'good', 'I am glad', 'Me too.', 

    'Then we are both glad', 'Indeed we are.', 

    'I sure hope this is enough fake conversation for stackoverflow.', 

    'Better write a few more messages just in case', 

    "But the message content isn't relevant", 'Oh yeah.', "I'm going to stop now.", 

    'redacted', 'redacted', 'redacted', 'redacted', 'redacted', 'redacted', 'redacted', 

    'redacted', 'redacted', 'redacted', 'redacted', 'redacted', 'redacted', 'redacted', 

    'redacted', 'redacted', 'redacted', 'redacted', 'redacted']}, 

    index = pd.DatetimeIndex([

    pd.Timestamp('2019-07-29 19:58:00'), pd.Timestamp('2019-07-29 20:03:00'), 

    pd.Timestamp('2019-08-01 19:22:00'), pd.Timestamp('2019-08-01 19:23:00'),

    pd.Timestamp('2019-08-01 19:25:00'), pd.Timestamp('2019-08-04 11:28:00'), 

    pd.Timestamp('2019-08-04 11:29:00'), pd.Timestamp('2019-08-04 11:29:00'), 

    pd.Timestamp('2019-08-04 12:43:00'), pd.Timestamp('2019-08-04 12:49:00'), 

    pd.Timestamp('2019-08-04 12:51:00'), pd.Timestamp('2019-08-04 12:51:00'), 

    pd.Timestamp('2019-08-25 22:33:00'), pd.Timestamp('2019-08-27 11:55:00'), 

    pd.Timestamp('2019-08-27 18:35:00'), pd.Timestamp('2019-11-06 18:53:00'), 

    pd.Timestamp('2019-11-06 18:54:00'), pd.Timestamp('2019-11-06 20:42:00'), 

    pd.Timestamp('2019-11-07 00:16:00'), pd.Timestamp('2019-11-07 15:24:00'), 

    pd.Timestamp('2019-11-07 16:06:00'), pd.Timestamp('2019-11-08 11:48:00'), 

    pd.Timestamp('2019-11-08 11:53:00'), pd.Timestamp('2019-11-08 11:55:00'), 

    pd.Timestamp('2019-11-08 11:55:00'), pd.Timestamp('2019-11-08 11:59:00'), 

    pd.Timestamp('2019-11-08 12:03:00'), pd.Timestamp('2019-12-24 13:40:00'), 

    pd.Timestamp('2019-12-24 13:42:00'), pd.Timestamp('2019-12-24 13:43:00'), 

    pd.Timestamp('2019-12-24 13:44:00'), pd.Timestamp('2019-12-24 13:44:00')]))


df_group = df.groupby(["sender"])

df_count = df_group[["sender"]].resample("D").count()


df_plot = pd.concat([df_count.loc['Person 1', :], 

                     df_count.loc['Person 2', :]], 

                    axis=1)

df_plot.columns = ['Sender 1', 'Sender 2']


df_plot.plot.area()

http://img1.sycdn.imooc.com//6320671b0001e4fb03610258.jpg

查看完整回答
反對(duì) 回復(fù) 2022-09-13
  • 1 回答
  • 0 關(guān)注
  • 74 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)