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

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

熊貓:如何處理奇怪的時(shí)間格式

熊貓:如何處理奇怪的時(shí)間格式

嗶嗶one 2022-12-20 16:35:47
我有以下 Pandas 數(shù)據(jù)框,其中時(shí)間(持續(xù)時(shí)間)以一種非常奇怪的格式給出:Person   Activity   Duration1        A          1 00:002        A          1 00:003        B          0 21:174        C          0 17:11其中1 00:00表示 24 小時(shí),0 21:17表示 0 天和 21:17 小時(shí),也就是說只有 21:17 小時(shí)。快速查看 dtypes 返回:In[1]: df.dtypesOut[1]: Person         objectActivity       objectDuration       objectdtype: object如果值為 ,我如何始終如一地對(duì)待該Duration列以返回 24 1 00:00,如果我有,則返回持續(xù)時(shí)間的十進(jìn)制值0 21:17?的十進(jìn)制值為0 21:1721.283。結(jié)果應(yīng)該是:Person   Activity   Duration1        A          242        A          243        B          21.2834        C          17.183
查看完整描述

5 回答

?
慕少森

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

a = np.array([24, 1, 1/60])    

df.Duration = df.Duration.str.split(' |:', expand=True).astype(int).dot(a)

例子:


df = pd.DataFrame({'Person': [1,2,3,4], "Activity": list('AABC') ,"Duration":['1 00:00', '1 00:00', '0 21:17', '0 17:11']})

df.Duration = df.Duration.str.split(' |:', expand=True).astype(int).dot(a)

print(df)

#   Person Activity   Duration

#0       1        A  24.000000

#1       2        A  24.000000

#2       3        B  21.283333

#3       4        C  17.183333


查看完整回答
反對(duì) 回復(fù) 2022-12-20
?
森欄

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

正如您提到的,它不會(huì)超過 1 00:00,即 24:00,有一種更簡(jiǎn)單的方法:


'''

Person  Activity    Duration

1   A   1 00:00

2   A   1 00:00

3   B   0 21:17

4   C   0 17:11

'''


import pandas as pd


df = pd.read_clipboard("\t")

.


   Person Activity Duration

0       1        A  1 00:00

1       2        A  1 00:00

2       3        B  0 21:17

3       4        C  0 17:11   

.


df['Duration'] = df['Duration'].str.split(' ')


df['Duration'] = ['24:00' if int(val[0]) == 1 else val[1] for val in df['Duration']]


print(df)

.


   Person Activity Duration

0       1        A    24:00

1       2        A    24:00

2       3        B    21:17

3       4        C    17:11


查看完整回答
反對(duì) 回復(fù) 2022-12-20
?
繁花不似錦

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

您可以輕松地將這些數(shù)字相乘和相加:


durations = [   

    "1 00:00",

    "0 21:17",          

]                                                                        


for duration in durations: 

    day, clock = duration.split()

    hour, minute = clock.split(':')

    print((int(day) * 24) + int(hour) + (int(minute) / 60))


查看完整回答
反對(duì) 回復(fù) 2022-12-20
?
青春有我

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

您可以使用 datetime 模塊進(jìn)行時(shí)間轉(zhuǎn)換


from datetime import datetime

def durationInDecimal(string):

    day, time = string.split(" ")

    t = datetime.strptime(time, "%H:%M").time()

    return int(day)*24 + (t.hour+t.minute/60.0)



df = pd.DataFrame({'Person': list("ABCD"), "Activity": list('ABCD') ,"duration":['1 00:00', '1 00:00', '0 21:17', '0 17:11']})

df["duration"] = df.duration.apply(durationInDecimal)


# Person    Activity    duration

# 0 A   A   24.000000

# 1 B   B   24.000000

# 2 C   C   21.283333

# 3 D   D   17.183333


查看完整回答
反對(duì) 回復(fù) 2022-12-20
?
明月笑刀無情

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

除了其他有用的答案之外,我還想發(fā)布我自己的解決方案,它使用自定義函數(shù)并將其應(yīng)用于數(shù)據(jù)框df.apply:


def custom_time_to_decimals(value):

    if value.split()[0]=='1':

        return 24

    else:

        custom = value.split()[1]

        hours = int(custom[0:2])

        minutes = int(custom[3:5])

        decimal = hours + (minutes/60)

        return round(decimal,3)


df['decimalHours'] = df['<insertYourTimeColumnHere>'].apply(custom_time_to_decimals)


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

添加回答

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