我的约会必须在水里岁月,我想找到一种方法,使列以09/30 / 1899_24:00开始,以9/30 / 1999_24:00结尾。 strong>
最初,我是这样的(下图),但是当我执行数据帧透视时,它弄乱了顺序。
这是我的代码片段
sim = pd.read_csv(headout,parse_dates=True, index_col='date')
sim['Layer'] = sim.groupby('date').cumcount() + 1
sim['Layer'] = 'L' + sim['Layer'].astype(str)
sim = sim.pivot(index = None , columns = 'Layer').T
sim = sim.reset_index()
sim = sim.rename(columns={"level_0": "NodeID"})
sim["NodeID"]= sim['NodeID'].astype('int64')
sim['gse'] = sim['NodeID'].map(sta.set_index(['NodeID'])['GSE'])
答案 0 :(得分:1)
24:00
不是有效时间'09/30/1899_24:00', '10/31/1899_24:00', '11/30/1898_24:00', '11/30/1899_24:00'
11/30/1898
在11/30/1899
之前24:00
替换为23:59
import pandas as pd
# dataframe
df = pd.DataFrame({'date': ['09/30/1899_24:00', '09/30/1899_24:00', '09/30/1899_24:00', '09/30/1899_24:00', '10/31/1899_24:00',
'10/31/1899_24:00', '10/31/1899_24:00', '10/31/1899_24:00', '11/30/1899_24:00', '11/30/1899_24:00']})
| | date |
|---:|:-----------------|
| 0 | 09/30/1899_24:00 |
| 1 | 09/30/1899_24:00 |
| 2 | 09/30/1899_24:00 |
| 3 | 09/30/1899_24:00 |
| 4 | 10/31/1899_24:00 |
| 5 | 10/31/1899_24:00 |
| 6 | 10/31/1899_24:00 |
| 7 | 10/31/1899_24:00 |
| 8 | 11/30/1899_24:00 |
| 9 | 11/30/1899_24:00 |
# replace 24:00
df.date = df.date.str.replace('24:00', '23:59')
# formate as datetime
df.date = pd.to_datetime(df.date, format='%m/%d/%Y_%H:%M')
# final
date
0 1899-09-30 23:59:00
1 1899-09-30 23:59:00
2 1899-09-30 23:59:00
3 1899-09-30 23:59:00
4 1899-10-31 23:59:00
5 1899-10-31 23:59:00
6 1899-10-31 23:59:00
7 1899-10-31 23:59:00
8 1899-11-30 23:59:00
9 1899-11-30 23:59:00
df.date = df.date.str.replace('_24:00', '')
df.date = pd.to_datetime(df.date, format='%m/%d/%Y')
date
0 1899-09-30
1 1899-09-30
2 1899-09-30
3 1899-09-30
4 1899-10-31
5 1899-10-31
6 1899-10-31
7 1899-10-31
8 1899-11-30
9 1899-11-30