【问题标题】:resulting in a Value Error when Appending groups to a pandas data frame [duplicate]将组附加到熊猫数据框时导致值错误[重复]
【发布时间】:2020-03-07 15:49:43
【问题描述】:

数据变量是我计算出的用于对每个输入进行排名的百分比。当我运行此代码时,它返回此错误。 “ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。” 请让我知道如何解决此问题或可能的帮助者。

谢谢!!

data on twitter stats

def create_column(data):
    if data <= 1.1175:
        grouping = 'not popular'
    elif data > 1.1176 and data < 2.235:
        grouping = 'Mildly not popular'  
    elif data > 2.236 and data < 3.3525:
        grouping = 'Mild Popularity'
    elif data >=4.47:
        grouping = 'popular'
    else:
        grouping = 'Neutral'
    return grouping
data = df1['score']

create_column((data))

【问题讨论】:

  • @abdoulsn 我读了一遍,还是有点困惑
  • 您能否发布您设置为创建 df1 的数据样本?
  • @oppressionslayer 添加了照片

标签: python pandas numpy


【解决方案1】:

如果我有你图片中的数据框,这应该适用于你的代码(如果没有,你可以为我以文本形式发布它,我想解决这个问题,熊猫是我的新瘾):

df = pd.DataFrame({'Retweets': [117489,117429,111489,113489],
                   'Score': [0.081086,0.081036,0.081286,0.021086]})

def create_column(data):
    if data <= 1.1175:
        grouping = 'not popular'
    elif data > 1.1176 and data < 2.235:
        grouping = 'Mildly not popular'  
    elif data > 2.236 and data < 3.3525:
        grouping = 'Mild Popularity'
    elif data >=4.47:
        grouping = 'popular'
    else:
        grouping = 'Neutral'
    return grouping

Popularity={}
for item in df.Score:
    Popularity.update({item:create_column(item)})

df['Popularity']=df['Score'].map(Popularity) 

【讨论】:

  • 完美运行,再次感谢。
猜你喜欢
  • 2021-09-24
  • 2017-06-13
  • 2020-02-01
  • 2018-02-08
  • 2014-01-03
  • 1970-01-01
  • 2019-01-14
  • 2019-10-22
  • 2018-10-14
相关资源
最近更新 更多