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

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

我將如何以不同的數(shù)量抵消 pandas 數(shù)據(jù)列?

我將如何以不同的數(shù)量抵消 pandas 數(shù)據(jù)列?

猛跑小豬 2023-05-16 15:12:51
我正在pandas使用matplotlib. 我有一個(gè)繪圖策略,我將歸零到初始值,然后將每個(gè)選定的變量偏移一個(gè)設(shè)定值。例如,這是我目前的繪圖方法:fig, ax = plt.subplots()# data is in a dataframe called inputDatatimeseries_plots=['var1','var3','var8']offsetFactor = 20for ii,var in enumerate(timeseries_plots)    offsetRef = inputData[var].loc[~inputData[var].isnull()].iloc[0]    ax.plot(inputData[TimeIndex], offsetFactor*(len(timeseries_plots_avg)-ii-1)+inputData[timeseries_plots_avg[ii]]-offsetRef, label=var,markersize=1,marker='None',linestyle = 'solid',color=colour)plt.show()這產(chǎn)生了這樣的東西(有一些matplotlib技巧):如您所見(jiàn),它刪除了offsetRef(在本例中為變量的初始值),然后offsetFactor向每個(gè)變量添加一個(gè)常量(在本例中等于 20)。結(jié)果是開始垂直偏移 20 的行。但是,當(dāng)值開始隨時(shí)間漂移并且一個(gè)變量可能與另一個(gè)變量交叉時(shí),這可能會(huì)成為一個(gè)問(wèn)題。我想做的是重置垂直偏移量 - 例如通過(guò)在特定日期之后更改 offsetRef。我試圖通過(guò)以下方式做到這一點(diǎn)。我首先初始化一個(gè)等于變量大小的數(shù)組。offsetRef然后我用在 處重新計(jì)算的值填充它resetDates。我已經(jīng)包含了注釋,這些注釋標(biāo)記了#PSEUDOCODE我大致寫下我想做的事情的地方——但提前道歉,因?yàn)樗鼈兎浅4植?。先感謝您!fig, ax = plt.subplots()inputData = pd.DataFrame(np.random.randint(100, size=(100, 5)), columns=['timestamp','var2','var3','var4','var5'])inputData['timestamp'][:]=pd.date_range('2020-may-01','2020-aug-08')timeseries_plots=['var1','var3','var4']offsetFactor = 20resetDates = ['2020-jun-23','2020-jul-05']for ii,var in enumerate(timeseries_plots)    offsetRef = np.zeros(inputData[var].size)    for tt,ttdate in enumerate(resetDates):        if tt=0:        #PSEUDO CODE: offsetRef[ inputData['timestamp'] <resetDates[tt]] = inputData[var].loc[~inputData[var].isnull()].iloc[0]        #PSEUDO CODE: offsetRef[ inputData['timestamp'] >=resetDates[tt]] = inputData[var].loc[~inputData[var].isnull()].iloc[ttdate]    #PSEUDO CODE: offsetRef[ inputData['timestamp'] >=resetDates[tt]] = inputData[var].loc[~inputData[var].isnull()].iloc[ttdate]        ax.plot(inputData[TimeIndex], offsetFactor*(len(timeseries_plots_avg)-ii-1)+inputData[timeseries_plots_avg[ii]]-offsetRef, label=var,markersize=1,marker='None',linestyle = 'solid',color=colour)plt.show()
查看完整描述

1 回答

?
當(dāng)年話下

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

這是我將堅(jiān)持的當(dāng)前解決方案,以便它可能對(duì)其他人有用:


fig, ax = plt.subplots()

# set up df

inputData = pd.DataFrame(np.random.randint(100, size=(100, 5)), columns=['timestamp','var2','var3','var4','var5'])

inputData['timestamp'][:]=pd.date_range('2020-may-01','2020-aug-08')

inputData['var2']=np.arange(0,100,1)

inputData['var2'][0:3]=49

inputData['var4']=np.arange(0,200,2)

inputData['var2'][0:3]=np.nan

# set constants and settings

dispFactor=20

timeseries_plots=['var2','var4']

resetDates=['2020-05-05','2020-05-20', '2020-08-04']

offsetFactor = dispFactor

#begin

fig, ax=plt.subplots()

for ii,var in enumerate(timeseries_plots):

    offsetRef = np.zeros(inputData[var].size)

    for tt,ttdate in enumerate(resetDates):

        if tt==0:        

            if inputData[var].loc[inputData['timestamp']==ttdate].isna().bool(): #if date is nan

                print('a',inputData[var].loc[~inputData[var].isnull()].iloc[0],inputData[var].bfill().loc[inputData['timestamp']==ttdate])

                offsetRef[(inputData['timestamp']<ttdate)]= inputData[var].loc[~inputData[var].isnull()].iloc[0]

                offsetRef[(inputData['timestamp']>=ttdate)]=inputData[var].bfill().loc[inputData['timestamp']==ttdate]

            else:

                print('b',inputData[var].loc[~inputData[var].isnull()].iloc[0],inputData[var].loc[inputData['timestamp']==ttdate])

                offsetRef[(inputData['timestamp']<ttdate)]= inputData[var].loc[~inputData[var].isnull()].iloc[0]

                offsetRef[(inputData['timestamp']>=ttdate)]= inputData[var].loc[inputData['timestamp']==ttdate]

        else:

            if inputData[var].loc[inputData['timestamp']==ttdate].isna().bool(): #if date is nan

                print('c')

                offsetRef[ inputData['timestamp'] >=resetDates[tt]] = inputData[var].bfill().loc[inputData['timestamp']==ttdate]

            else:

                print('d',inputData[var].loc[inputData['timestamp']==ttdate])

                offsetRef[ inputData['timestamp'] >=resetDates[tt]] = inputData[var].loc[inputData['timestamp']==ttdate]

        print(offsetRef)

    ax.plot(inputData['timestamp'], offsetFactor*(len(timeseries_plots)-ii-1)+inputData[var]-offsetRef)


plt.show()

這會(huì)將所選位置的偏移量“重置”為 20,resetDates以生成下圖:

http://img1.sycdn.imooc.com//64632d190001379106460465.jpg

在任何一種情況下,我可能都不需要 nan 數(shù)據(jù)的 if 邏輯捕獲(并且只依賴.bfill())來(lái)工作 - 但這讓我覺(jué)得它更安全。我將在改進(jìn)解決方案時(shí)進(jìn)行編輯。



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

添加回答

舉報(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)