我是 Python/pandas 的新手。我有一個看起來像這樣的資料框
x = pd.DataFrame([20210901,20210902, 20210903, 20210904])
[出去]:
0
0 20210901
1 20210902
2 20210903
3 20210904
我想按如下方式分隔每一行:例如
year = 2021
month = 9
day = 1
或者我有這樣的每一行的串列:
[2021,9,1]
uj5u.com熱心網友回復:
您可以使用pd.to_datetime將整個列轉換為datetime型別。
>>> import pandas as pd
>>>
>>> df = pd.DataFrame({'Col': [20210917,20210918, 20210919, 20210920]})
>>>
>>> df.Col = pd.to_datetime(df.Col, format='%Y%m%d')
>>> df
Col
0 2021-09-17
1 2021-09-18
2 2021-09-19
3 2021-09-20
>>> df['Year'] = df.Col.dt.year
>>> df['Month'] = df.Col.dt.month
>>> df['Day'] = df.Col.dt.day
>>>
>>> df
Col Year Month Day
0 2021-09-17 2021 9 17
1 2021-09-18 2021 9 18
2 2021-09-19 2021 9 19
3 2021-09-20 2021 9 20
如果您希望結果為串列,則可以將串列理解與zip函式一起使用。
>>> [(year, month, day) for year, month, day in zip(df.Year, df.Month, df.Day)]
[(2021, 9, 17), (2021, 9, 18), (2021, 9, 19), (2021, 9, 20)]
uj5u.com熱心網友回復:
將每一行按如下三個新列分開:年、月、日
import pandas as pd
df = pd.DataFrame({"date":[20210901,20210902, 20210903, 20210904]})
# split date field to three fields: year month day
df["year"] = df["date"].apply(lambda x: str(x)[:4])
df["month"] = df["date"].apply(lambda x: str(x)[4:6])
df["day"] = df["date"].apply(lambda x: str(x)[6:])
print(df)
結果如下
# result is:
date year month day
0 20210901 2021 09 01
1 20210902 2021 09 02
2 20210903 2021 09 03
3 20210904 2021 09 04
uj5u.com熱心網友回復:
我創建了一個簡單的函式,它將以您想要的格式回傳一個壓縮串列
def convert_to_dates(df):
dates = []
for i,v in x.iterrows():
dates.append(v.values[0])
for i in range(len(dates)):
dates[i] = str(dates[i])
years = []
months = []
days = []
for i in range(len(dates)):
years.append(dates[i][0:4])
months.append(dates[i][4:6])
days.append(dates[i][6:8])
return list(zip(years, months, days))
使用convert_to_dates(x)
輸出呼叫它:
In [4]: convert_to_dates(x)
Out[4]:
[('2021', '09', '01'),
('2021', '09', '02'),
('2021', '09', '03'),
('2021', '09', '04')]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/366056.html
上一篇:在R中填寫日期
