在datafram中查找列表中的最后一个值

2024-03-29 07:23:44 发布

您现在位置:Python中文网/ 问答频道 /正文

求你了,我想在一个数据帧中找到in client的最后一个值,我该怎么做?你知道吗

示例:

df = pd.DataFrame({'date': 
    ['2018-06-13', '2018-06-14', '2018-06-15', '2018-06-16'],
    'gain': [[10, 12, 15],[14, 11, 15],[9, 10, 12], [6, 4, 2]],
    'how':  [['customer1', 'customer2', 'customer3'], 
            ['customer4','customer5','customer6' ],
            ['customer7', 'customer8', 'customer9'],
            ['customer5', 'customer6', 'customer10'] ]}


   df : 
           date       gain                    how
    0 2018-06-13  [10, 12, 15]    [customer1, customer2, customer3]
    1 2018-06-14  [14, 11, 15]    [customer4, customer5, customer6]
    2 2018-06-15  [9, 10, 12]     [customer7, customer8, customer9]
    3 2018-06-16  [6, 4, 2]       [customer5, customer6, customer10]
I want to do a function that returns the last gain in the dataframe.
example :
 for the customer5 = 6
 for the customer4 = 14
 for the customer20 = 'not found' 

非常感谢


Tags: theindffordatehowgaincustomer1
1条回答
网友
1楼 · 发布于 2024-03-29 07:23:44

然后使用unnesting函数,drop_duplicates

newdf=unnesting(df,['gain','how']).drop_duplicates('how',keep='last')
newdf
Out[25]: 
   gain         how        date
0    10   customer1  2018-06-13
0    12   customer2  2018-06-13
0    15   customer3  2018-06-13
1    14   customer4  2018-06-14
2     9   customer7  2018-06-15
2    10   customer8  2018-06-15
2    12   customer9  2018-06-15
3     6   customer5  2018-06-16
3     4   customer6  2018-06-16
3     2  customer10  2018-06-16

然后用reindex输入搜索列表

l=['customer5','customer6','customer20']

newdf.loc[newdf.how.isin(l)].set_index('how').reindex(l,fill_value='not_find')
Out[34]: 
                gain        date
how                             
customer5          6  2018-06-16
customer6          4  2018-06-16
customer20  not_find    not_find

关于这类问题解答的有趣读物

How do I unnest a column in a pandas DataFrame?

def unnesting(df, explode):
    idx=df.index.repeat(df[explode[0]].str.len())
    df1=pd.concat([pd.DataFrame({x:np.concatenate(df[x].values)} )for x in explode],axis=1)
    df1.index=idx
    return df1.join(df.drop(explode,1),how='left')

相关问题 更多 >