python - 查找数据框中列表中的最后一个值
问题描述
拜托,我想在数据框中找到客户端的最后一个值,我该怎么做?
例子 :
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'
太感谢了
解决方案
然后使用取消嵌套功能,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
关于此类问题的解决方案的有趣阅读
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')
推荐阅读
- tomcat - 将 Jena Fuseki 的 CURL 访问限制为仅对 localhost
- primefaces - 在 p:dataTable 的页脚显示过滤结果大小
- ios - 如何更改ios中uitoolbar中按钮的位置并为按钮设置徽章以显示计数
- javascript - 如何在小屏幕上折叠侧边栏?
- r - 如何按可变列数对数据框进行排序?
- php - preg_replace UTF-8 BOM Unicode 字符
- vba - 为什么它出来的变量未设置?
- c - 在 tmp 文件夹中打开文件时使用信号量不起作用
- java - Hashmap 获取标签崩溃
- asynchronous - 如何异步检索数据并使用基于 Tokio 的回显服务器对其进行修改?