我試圖從整數格式的列中獲取和月份
我試圖從整數格式的列中獲取和月份。
''dvd['yy'] = str(dvd['CalendarYearMonth']) [:3]
dvd['mon'] = str(dvd['CalendarYearMonth'])[4:6]''
但是得到以下輸出
CalendarYearMonth CountryCode Dividends yy mon
0 202108 CN 196.0 0 2
1 202109 CN 380.0 0 2
2 202108 In NaN 0 2
3 202109 IN 115.0 0 2
誰能幫助我獲得正確的輸出--dvd是輸入DF
uj5u.com熱心網友回復:
試一下這個:
dvd['yy'] = dvd['CalendarYearMonth'] 。 astype(str).str[:3]
dvd['mon'] = dvd['CalendarYearMonth'].astype(str)。 str[4:6]
uj5u.com熱心網友回復:
試試這個:
dvd = pd.DataFrame( {
'CalendarYearMonth'/span>: [202108.0, 202109. 0, 202108.0, 202109.0] 。
})
dvd['yy'] = dvd['CalendarYearMonth'] 。 apply(lambda x : str(x)[:4])
dvd['mon'] = dvd['CalendarYearMonth'] 。 apply(lambda x : str(x) [4:6]
print(DVD)
輸出:
CalendarYearMonth yy mon
0 202108.0 2021 08
1 202109.0 2021 09
2 202108.0 2021 08
3 202109.0 2021 09
uj5u.com熱心網友回復:
如果日期已經是一個int,就利用它吧
df['yy'] = df['CalendarYearMonth']//100>
df['mon'] = df['CalendarYearMonth']-df['yy']*100
輸出:
CalendarYearMonth CountryCode Dividends yy mon
0 202108 CN 196.0 2021 8
1 202109 CN 380.0 2021 9
2 202108 In NaN 2021 8
3 202109 IN 115.0 2021 9
uj5u.com熱心網友回復:
再提一個選項,你可以把帶有日期的列轉換為資料時間物件,然后提取年和月的資訊:
import pandas as pd
dvd = pd.DataFrame({
'CalendarYearMonth': [201908, 202001, 202103, 2107] 。
})
dates = pd.to_datetime(dvd['CalendarYearMonth'], format='%Y%m')
dvd['yy'] = dates.dt.year
dvd['mon'] = dates.dt.month
它給出了:
CalendarYearMonth yy mon
0 201908 2019 8
1 202001 2020 1
2 202103 2021 3
3 202107 2021 7
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/312014.html
標籤:
