Python 检索表中最后一个月的事务的列名
假设我有一个数据帧,格式如下:Python 检索表中最后一个月的事务的列名,python,pandas,Python,Pandas,假设我有一个数据帧,格式如下: id | name | 052017 | 062017 | 072017 | 092017 | 102017 20 | abcd | 0 | 100 | 200 | 50 | 0 我需要检索组织上一个月的任何事务的列名。在本例中,我想添加一个名为“date_string”的列,其内容为092017 有什么办法可以做到这一点 谢谢 将0替换为np.nan,然后使用last\u valid\u index df.replace(0,n
id | name | 052017 | 062017 | 072017 | 092017 | 102017
20 | abcd | 0 | 100 | 200 | 50 | 0
我需要检索组织上一个月的任何事务的列名。在本例中,我想添加一个名为“date_string”的列,其内容为092017
有什么办法可以做到这一点
谢谢
将0替换为np.nan
,然后使用last\u valid\u index
df.replace(0,np.nan).apply(lambda x :x.last_valid_index(),1)
Out[602]:
0 092017
dtype: object
#df['newcol'] = df.replace(0,np.nan).apply(lambda x :x.last_valid_index(),1)