Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检索表中最后一个月的事务的列名_Python_Pandas - Fatal编程技术网

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)