创建从分析的数据框派生的两个列表
Posted
技术标签:
【中文标题】创建从分析的数据框派生的两个列表【英文标题】:Creating two lists derived from an analysed dataframe 【发布时间】:2022-01-18 21:00:52 【问题描述】:我正在创建一个函数,该函数可以检查我的数据集的多个条件,然后返回两个列表。
清单 1:执行 X 的项目
清单 2:要执行的项目 Y
样本数据:
Product Variable Value
0 A Sell 0.1
1 B Keep -1.2
2 C Sell 0.3
3 D Swap 0.1
4 E Swap 0.1
理想的结果:
Sell; A, C
Swap; 'Nothing to swap this week'
运行时出错:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
到目前为止我的代码:
def weekly_forcasting(df):
a = df['Swap or Sell']
b = df['Current Value']
if (a == 'Sell') and (b > 0).any():
return ('Product_Label')
if (a == 'Sell') and (b < 0).any():
return 'Nothing to sell this week'
if (a == 'Swap') and (b > 0).any():
return 'Nothing to swap this week'
if (a == 'Swap') and (b < 0).any():
return ('Product_Label')
【问题讨论】:
【参考方案1】:b = df['Current Value']
使b
成为错误消息所暗示的系列。所以理想情况下你想使用.loc
,例如你可能想要
selling_list = (
df.loc[
(df["Swap or Sell"]=="Sell") & (df["Current Value"]>0)
]["Product_Label"].tolist() if
len(df.loc[
(df["Swap or Sell"]=="Sell") & (df["Current Value"]>0)
]["Product_Label"].tolist()) > 0
else "nothing to sell this week"
)
同样你可以定义swapping_list
并做一个return selling_list, swapping_list
【讨论】:
【参考方案2】:尝试使用numpy.select
import numpy as np
# create a list of condtions
conditions = [
(df['Swap or Sell'] == 'Sell') & (df['Current Value'] > 0),
(df['Swap or Sell'] == 'Sell') & (df['Current Value'] < 0),
(df['Swap or Sell'] == 'Swap') & (df['Current Value'] > 0),
(df['Swap or Sell'] == 'Swap') & (df['Current Value'] < 0),
]
# create a list of chices that mtach a statisifed condition
choices = [
df['Product_Label'],
'There are no Sell recommendations today',
'There are no Swap recommendations today',
df['Product_Label'],
]
# create a new by using numpy.select
df['data'] = np.select(conditions, choices, 'Keep')
然后只需使用布尔索引访问您想要的信息
df.loc[df['Swap or Sell'] == 'Sell', 'data']
【讨论】:
以上是关于创建从分析的数据框派生的两个列表的主要内容,如果未能解决你的问题,请参考以下文章