我有此列标题:
我用x = list(df)
将它们放在列表中,并且x
的类型是字符串。我想将它们放入另一个字符串列表中,但将它们转换为8 / 18、7 / 18、6 / 18等...
这是df.head()输出:
print(df.columns)
的输出
<class 'str'>
Index(['NDD 8/31', 'Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'May 2018(P&I Applied)',
'Apr 2018(P&I Applied)', 'Mar 2018(P&I Applied)',
'Feb 2018(P&I Expected)', 'Jan 2018(P&I Applied)',
'Dec 2017(P&I Applied)', 'Nov 2017(P&I Applied)',
'Oct 2017(P&I Applied)', 'Sep 2017(P&I Applied)',
'Paystring as of cut off as if 10.31',
'Paystrings as of
有人知道如何实现这一目标吗?
答案 0 :(得分:1)
您可以转换为datetime
,然后使用strftime
。下面的逻辑还可以确保非日期列保持不变。
df = pd.DataFrame(columns=['Aug 2018(P&I Applied)', 'Jul 2018(P&I Applied)',
'Jun 2018(P&I Applied)', 'Paystring as of cut off as if 10.31'])
dates = pd.Series(pd.to_datetime(df.columns.str[:8], errors='coerce'))
df.columns = dates.dt.strftime('%m/%y').mask(dates.isnull(), pd.Series(df.columns))
print(df.columns)
# Index(['08/18', '07/18', '06/18', 'Paystring as of cut off as if 10.31'], dtype='object')
print(dates.dt.month)
# 0 8.0
# 1 7.0
# 2 6.0
# 3 NaN
# dtype: float64