gpt4 book ai didi

python - 如何有条件地将值分配给列?

转载 作者:行者123 更新时间:2023-12-01 06:41:56 25 4
gpt4 key购买 nike

 Customer_ID     Gender  First_Date First_region  First_state        First_city  \
0 129609144 M 20130130 West Gujarat Surat
1 129627580 M 20130129 North Delhi Delhi
2 130363481 M 20130221 West Gujarat Surat
3 49817480 M 20130222 West Maharashtra Pimpri-Chinchwad
4 126343829 F 20130301 North Delhi Delhi

Recent_Date Last_region Last_state Last_city Customer_Value \
0 20130216 West Gujarat Surat 2032.0
1 20130129 North Delhi Delhi 1709.0
2 20130221 West Gujarat Surat 523.0
3 20130222 West Maharashtra Pimpri-Chinchwad 5132.0
4 20130301 North Delhi Delhi 1008.0

Buy_Times Points_Earned Points_Redeemed
0 2 200.0 0.0
1 1 100.0 0.0
2 1 10.0 0.0
3 1 170.0 0.0
4 1 60.0 0.0

我正在尝试创建一个新的列名称“客户值(value)分割”,但我想根据“Customer_Value”列的值分配此列中的值。

所以,

  • 高值(value)分割市场 -> 25000
  • 中值分割市场 - 10000 之间和25000
  • 低值(value)分割 - 小于或等于 10000

我尝试过这个方法:

df['customer value segment'] = np.where(df['Customer_Value'] > 25000, 'High Value Segment', np.where(10000 > df['Customer_Value'] > 25000, 'Medium Value Segment', np.where(df['Customer_Value'] <= 10000, 'Low Value Segment', 'None')))  

但是,运气不佳。它向我抛出以下错误:

 ValueError                                Traceback (most recent call last)
<ipython-input-48-fee1062f32ba> in <module>
----> 1 df['customer value segment'] = np.where(df['Customer_Value'] > 25000, 'High Value Segment', np.where(10000 > df['Customer_Value'] > 25000, 'Medium Value Segment', np.where(df['Customer_Value'] <= 10000, 'Low Value Segment', 'None')))

~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1476 raise ValueError("The truth value of a {0} is ambiguous. "
1477 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478 .format(self.__class__.__name__))
1479
1480 __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我现在应该如何处理这个问题?

注意 - 如果您想读取实际数据集,我就是这样做的:

df = pd.read_csv('Customers.csv', encoding='unicode_escape')

最佳答案

这应该有效:

df.loc[df['Customer_Value'] > 25000, 'customer value segment'] = 'High Value Segment' 
df.loc[(df['Customer_Value'] >= 10000) & (df['Customer_Value'] <= 25000) , 'customer value segment'] = 'Medium Value Segemnt '
df.loc[df['Customer_Value'] < 10000, 'customer value segment'] = 'Low Value Segment '

关于python - 如何有条件地将值分配给列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59417480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com