简体   繁体   English

如何 select 从最后一行到第一行的列名具有有效值?

[英]How to select the column name from the last and the first row with valid values?

I wish to know when one ID become a member in company and when he left, I thought in transpose my table, but I have 40000 entries.我想知道一个ID什么时候成为公司的成员,什么时候离开,我想转置我的表,但我有40000个条目。 Someone have a insight to help me?有人有见识可以帮助我吗?

The following dataframe without the comlumns 'Fisrt_Entry_Month' and 'Last_Entry_Month' is an exemple, If possible I wish to get the information ['Fisrt_Entry_Month' and 'Last_Entry_Month'] like my scheme.下面的 dataframe 没有 'Fisrt_Entry_Month' 和 'Last_Entry_Month' 是一个例子,如果可能的话,我希望像我的方案一样获得信息 ['Fisrt_Entry_Month' 和 'Last_Entry_Month']。

        'JAN': ['0', '1','1','0','0','1','0'],
        'FEB': ['0', '1','1','1','1','0','0'],
        'MAR': ['0', '0','0','0','0','0','0'],
        'APR': ['0', '0','1','0','0','0','1'],
        'MAI': ['0', '1','0','0','1','1','1'],
        'Fisrt_Entry_Month': ['Nan', 'JAN','JAN','FEB','FEB','JAN','APR'],
        'Last_Entry_Month': ['Nan', 'MAI','APR','FEB','MAI','MAI','MAI'], 
        }

desired = pd.DataFrame (desired, columns = ['ID', 'JAN','FEB','MAR','APR','MAI','Fisrt_Entry_Month','Last_Entry_Month'])
    ID  JAN FEB MAR APR MAI Fisrt_Entry_Month   Last_Entry_Month
0   1   0   0   0   0   0   Nan Nan
1   2   1   1   0   0   1   JAN MAI
2   3   1   1   0   1   0   JAN APR
3   4   0   1   0   0   0   FEB FEB
4   5   0   1   0   0   1   FEB MAI
5   6   1   0   0   0   1   JAN MAI
6   7   0   0   0   1   1   APR MAI

Use idxmax :使用idxmax

df['Fisrt_Entry_Month'] = df.iloc[1:, :5].mask(df.iloc[1:, :5].eq('0'))\
                            .astype(float).idxmax(1)
df['Last_Entry_Month'] = df.iloc[1:, :5].mask(df.iloc[1:, :5].eq('0'))\
                           .astype(float).iloc[:,::-1].idxmax(1)

By creating a DataFrame from your dictionary you can move on to just reading desired['First_Entry_Month', 'Last_Entry_Month'] .通过从字典中创建DataFrame ,您可以继续阅读desired['First_Entry_Month', 'Last_Entry_Month'] Here's how you create the DataFrame以下是创建DataFrame的方法

dictionary = {  'JAN': ['0', '1','1','0','0','1','0'],
        'FEB': ['0', '1','1','1','1','0','0'],
        'MAR': ['0', '0','0','0','0','0','0'],
        'APR': ['0', '0','1','0','0','0','1'],
        'MAI': ['0', '1','0','0','1','1','1'],
        'Fisrt_Entry_Month': ['Nan', 'JAN','JAN','FEB','FEB','JAN','APR'],
        'Last_Entry_Month': ['Nan', 'MAI','APR','FEB','MAI','MAI','MAI'], 
        }
desired = pd.DataFrame.from_dict(dictionary)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM