【问题标题】:How to return a value based on column value and Timestamp using user-defined function in pandas如何在 Pandas 中使用用户定义的函数根据列值和时间戳返回值
【发布时间】:2021-04-01 05:38:15
【问题描述】:

我有两个数据框,我已加入。在加入的 Dataframe 上,我正在编写一个用户定义的函数,其中基于时间戳和列的值计数,我需要根据下面提到的条件返回值,创建一个名为“Day_Sentiment”的新列。但我得到了错误。请告诉我该怎么做

输入:

                  Date          Content    Cleaned-content   Sentiment   
                  11/12/2020    abb        bbc abb           Bad         
                  12/10/2020    xyz        xxy               Good        
                  11/24/2020    tyu        yuu               Neutral     
                  12/16/2020    iop        yui               Bad         

输出:

               Date          Content    Cleaned-content   Sentiment   Day_Sentiment
               11/12/2020    abb        bbc abb           Bad         Bad
               12/10/2020    xyz        xxy               Good        Bad
               11/24/2020    tyu        yuu               Neutral     Bad
               12/16/2020    iop        yui               Bad         Bad

到目前为止,我在下面尝试过:

df = input_data.join(results)

def compare_def(df):

    no.bad_senti= df.loc[df['Sentiment'] == 'Bad']
    no.neut_senti = df.loc[df['Sentiment'] == 'Neutral']
    no.good_senti= df.loc[df['Sentiment'] == 'Good']

    if ((no.bad_senti> no.good_senti) & (no.bad_senti> no.neut_senti)):
       output = 'Bad'
    elif ((no.good_senti> no.bad_senti) & (no.good_senti> no.neut_senti)):
       output= 'Good'
    elif ((no.neut_senti> no.bad_senti) & (no.neut_senti> no.good_senti)):
       output= 'Neutral'
    elif no.good_senti== no.bad_senti:
       output= 'Neutral'
    elif no.bad_senti== no.neut_senti:
       output= 'bad'
    elif no.good_senti== no.neut_senti:
       output= 'good'
    else:
       output= 'Neutral'

    return output

df['Day_Sentiment'] = output

替代:

 output = compare_def(df)
 df['Day_Sentiment'] = output

错误:

     ValueError: Can only compare identically-labeled DataFrame objects

示例 1: 预测情绪 情绪 2 坏 1 好 1 中性

然后在函数中 2 > 1 和 2 > 1 返回错误

示例 2: 情绪: 2 坏 5 好 5 中性

功能:

2 > 5 错误的 5 > 2 和 5 > 5 错误的 5 > 2 和 5 > 5 错误的 5==2 错误的 2==5 错误的 5==5 真的 回报好

【问题讨论】:

    标签: python python-3.x pandas dataframe valueerror


    【解决方案1】:

    您的代码存在几个问题。首先,变量 bad、good 和 neut 是包含字符串变量的不同长度的 Panda 系列。然后,您尝试评估执行几个条件测试,例如if ((bad> good) & (bad> neut),它会生成您的 ValueError。我不太确定您尝试实现什么逻辑,但以下模板可能会有所帮助:

    def compare_data(row):
        value = 'Good'
        # The logic here escapes me
        # Evaluate the row contents of row[Sentiment] and modify value
        return value  
    
    df["Day Sentiment"]= df.apply(lambda row: compare_data(row), axis= 1)
    

    产量:

        Date    Content Cleaned-content Sentiment   Day Sentiment
    0   11/12/2020  abb bbc abb Bad Good
    1   12/10/2020  xyz xxy Good    Good
    2   11/24/2020  tyu yuu Neutral Good
    3   12/16/2020  iop yui Bad Good
    

    【讨论】:

    • 谢谢,但价值并不总是好的。它也可能是坏的和中立的
    • 我已经编辑了结果以获得更好的清晰度
    • 我意识到这一点,但无法确定您确定值的逻辑,正如我在 compare_data 函数中指出的那样。如果你能解释设置值背后的逻辑,我会很乐意编辑代码/
    • 是的,当然,所以每天都有来自不同来源的新闻,我为此建立了情绪模型。现在,该任务基于对特定日期的情绪预测,我需要根据函数中提到的条件返回一个情绪,并将其写入一个名为 'Day_Sentiment' 的列。例如,一天我得到 5 行数据,其中 2 行预测为 Good,1 行预测为 Bad,2 行预测为 Neutral。我需要使用上面的函数作为三个中的一个返回
    • 好的,我了解 Sentiment 列,但是您要执行的功能是什么?您在哪里按日期对数据进行分组?在提供的示例中,我不明白这个函数是如何工作的?
    猜你喜欢
    • 2014-11-01
    • 1970-01-01
    • 2023-02-08
    • 2021-09-01
    • 1970-01-01
    • 2019-01-15
    • 2020-04-16
    • 2018-08-29
    • 1970-01-01
    相关资源
    最近更新 更多