简体   繁体   English

如何基于其他三列之一创建新列?

[英]How to create a new column based on one of three other columns?

I have a Dataframe that has a movie name column and 3 other columns (let's call them A, B, and C) that are ratings from 3 different sources. 我有一个数据帧,其中包含电影名称列和其他3个列(分别称为A,B和C),它们分别来自3个不同来源。 There are many movies with only one rating, some movies with a combination from the 3 forums, and some with no ratings. 许多电影只有一个等级,有些电影是来自3个论坛的组合,有些则没有评级。 I want to create a new column that will: 我想创建一个新列,该列将:

  1. If A column has associated rating, use A. 如果A列具有关联的评分,请使用A。
  2. If A column is empty, get associated rating from B. 如果A栏为空,请从B获得相关的评级。
  3. If B column is empty, get associated rating from C. 如果B列为空,请从C获取相关的评分。
  4. If C column is empty, return "Unrated" 如果C列为空,则返回“未分级”

This is what I have in my code so far: 到目前为止,这就是我的代码:

def check_rating(rating):
    if newyear['Yahoo Rating'] != "\\N":
        return rating
    else:
        if newyear['Movie Mom Rating'] != "\\N":
            return rating
        else:
            if newyear['Critc Rating'] != "\\N":
                return rating
            else:
                return "Unrated"

df['Rating'] = df.apply(check_rating, axis=1)

The error I get is: 我得到的错误是:

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

For visual of my dataframe, here is newyear.head() : 为了显示我的数据newyear.head() ,这里是newyear.head()

新年数据框

I am not sure what this value error means to fix this problem and also if this is the right way to do it. 我不确定此值错误对解决此问题意味着什么,也不确定这样做是否正确。

I would do something like this: 我会做这样的事情:

df = df.replace('\\N', np.nan)  # this requires import numpy as np
(df['Yahoo Rating'].fillna(df['Movie Mom Rating']
                   .fillna(df['Critic Rating']
                   .fillna("Unrated"))))

The reason that your code doesn't work is that newyear['Yahoo Rating'] != "\\\\N" is a boolean array. 您的代码不起作用的原因是newyear['Yahoo Rating'] != "\\\\N"是一个布尔数组。 What you say here is something like if [True, False, True, False]: . 您在这里说的话类似if [True, False, True, False]: That's the source of ambiguity. 这就是模棱两可的根源。 How do you evaluate such a condition? 您如何评估这种情况? Would you execute if all of them True or would just one of them be enough? 如果它们全部为True,您将执行该命令还是仅其中之一就足够了?

As M. Klugerford explained , you can change it so it is evaluated row by row (therefore returns a single value). 正如M. Klugerford解释的那样 ,您可以对其进行更改,以便逐行对其进行求值(因此将返回单个值)。 However, row by row apply operations are generally slow and pandas has great tools for handling missing data. 但是,逐行应用操作通常速度较慢,并且熊猫具有出色的工具来处理丢失的数据。 That's why I am suggesting this. 这就是为什么我建议这样做。

You are returning rating in your original function .. but rating is the row , not the value of any column 您将在原始函数中返回rating ..但rating ,而不是任何列的值

>>> df
    A   B   C Genre Title Year
0   7   6  \N    g1    m1   y1
1  \N   5   7    g2    m2   y2
2  \N  \N  \N    g3    m3   y3
3  \N   4   1    g4    m4   y4
4  \N  \N   3    g5    m5   y5

>>> def rating(row):
    if row['A'] != r'\N':
        return row['A']
    if row['B'] != r'\N':
        return row['B']
    if row['C'] != r'\N':
        return row['C']
    return 'Unrated'

>>> df['Rating'] = df.apply(rating, axis = 1)
>>> df
    A   B   C Genre Title Year   Rating
0   7   6  \N    g1    m1   y1        7
1  \N   5   7    g2    m2   y2        5
2  \N  \N  \N    g3    m3   y3  Unrated
3  \N   4   1    g4    m4   y4        4
4  \N  \N   3    g5    m5   y5        3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM