【问题标题】:unable to return values for new column in dataframe无法返回数据框中新列的值
【发布时间】:2017-11-26 14:10:46
【问题描述】:

我正在尝试向我的数据框添加一个额外的列。我使用基于其他变量var1var2if/else 条件,并尝试将return 值放入这个新列prediction。通过使用以下代码,我可以创建一个列名,但是由于某种我无法弄清楚的原因,新列是 EMPTY。没有出现错误,所以我认为return 有问题?感谢您的帮助!

这是我的数据框和代码的子集:

var1    var2    choice    prediction
-1.7     0       TRUE     
3.5      0       TRUE
1.2      0       FALSE      #empty#   
6.7      0       FALSE
-0.6     1       TRUE
-2.8     1       FALSE
2.1      1       TRUE

def prediction(row):
    if row['var1'] > row['var2']:
        if row['choice'] == "TRUE": # "TRUE" and "FALSE" are bool.
            return 'miss'   # return values and add into the new column
        elif row['choice'] == "FALSE":
            return 'match'

    elif row['var1'] < row['var2']:
        if row['choice'] == "FALSE":
            return 'miss'
        elif row['choice'] == "TRUE":
            return 'match'

    else:
        if row['choice'] == "TRUE":
            return 'match'
        elif row['choice'] == "FALSE":
            return 'miss' 

df['prediction'] = df.apply(lambda row: prediction(row), axis=1)

【问题讨论】:

  • 值是字符串 'TRUE''FALSE' 或布尔值 TrueFalse ?
  • df.dtypes 是什么?
  • @jezrael 抱歉,我不清楚。不,它们是字符串。不确定df.dtypes 是什么意思,但df 表示我的数据框的名称
  • print (df.dtypes) 是什么?

标签: python pandas dataframe return


【解决方案1】:

看来你的数据是布尔值有问题,没有字符串TRUEFALSE,所以内部条件永远不会返回True

np.random.seed(123)
N = 10
df = pd.DataFrame({'var1':np.random.randint(10, size=N),
                   'var2':np.random.randint(10, size=N), 
                   'choice':np.random.choice([True, False], size=N)})
print (df)
   choice  var1  var2
0   False     2     9
1   False     2     0
2    True     6     0
3    True     1     9
4   False     3     3
5    True     9     4
6    True     6     0
7   False     1     0
8    True     0     4
9   False     1     1

print (df.dtypes)
choice     bool
var1      int32
var2      int32
dtype: object

def prediction(row):
    if row['var1'] > row['var2']:
        if row['choice'] == "TRUE":
            return 'miss'   # return values and add into the new column
        elif row['choice'] == "FALSE":
            return 'match'

    elif row['var1'] < row['var2']:
        if row['choice'] == "FALSE":
            return 'miss'
        elif row['choice'] == "TRUE":
            return 'match'

    else:
        if row['choice'] == "TRUE":
            return 'match'
        elif row['choice'] == "FALSE":
            return 'miss' 

df['prediction'] = df.apply(lambda row: prediction(row), axis=1)
print (df)
   choice  var1  var2 prediction
0   False     2     9       None
1   False     2     0       None
2    True     6     0       None
3    True     1     9       None
4   False     3     3       None
5    True     9     4       None
6    True     6     0       None
7   False     1     0       None
8    True     0     4       None
9   False     1     1       None

所以需要:

def prediction(row):
    if row['var1'] > row['var2']:
        if row['choice'] == True:
            return 'miss'   # return values and add into the new column
        elif row['choice'] == False:
            return 'match'

    elif row['var1'] < row['var2']:
        if row['choice'] == False:
            return 'miss'
        elif row['choice'] == True:
            return 'match'

    else:
        if row['choice'] == True:
            return 'match'
        elif row['choice'] == False:
            return 'miss' 

df['prediction'] = df.apply(lambda row: prediction(row), axis=1)
print (df)
   choice  var1  var2 prediction
0   False     2     9       miss
1   False     2     0      match
2    True     6     0       miss
3    True     1     9      match
4   False     3     3       miss
5    True     9     4       miss
6    True     6     0       miss
7   False     1     0      match
8    True     0     4      match
9   False     1     1       miss

另一个可能的问题是,如果值不匹配 - 字符串 'True''TRUE' 并与 False 类似:

np.random.seed(123)
N = 10
df = pd.DataFrame({'var1':np.random.randint(10, size=N),
                   'var2':np.random.randint(10, size=N), 
                   'choice':np.random.choice(['True', 'False'], size=N)})
print (df)
  choice  var1  var2
0  False     2     9
1  False     2     0
2   True     6     0
3   True     1     9
4  False     3     3
5   True     9     4
6   True     6     0
7  False     1     0
8   True     0     4
9  False     1     1

def prediction(row):
    if row['var1'] > row['var2']:
        if row['choice'] == "TRUE":
            return 'miss'   # return values and add into the new column
        elif row['choice'] == "FALSE":
            return 'match'

    elif row['var1'] < row['var2']:
        if row['choice'] == "FALSE":
            return 'miss'
        elif row['choice'] == "TRUE":
            return 'match'

    else:
        if row['choice'] == "TRUE":
            return 'match'
        elif row['choice'] == "FALSE":
            return 'miss' 

df['prediction'] = df.apply(lambda row: prediction(row), axis=1)
print (df)
  choice  var1  var2 prediction
0  False     2     9       None
1  False     2     0       None
2   True     6     0       None
3   True     1     9       None
4  False     3     3       None
5   True     9     4       None
6   True     6     0       None
7  False     1     0       None
8   True     0     4       None
9  False     1     1       None

【讨论】:

  • 太棒了!感谢您通过示例进行详细解释!我的错,TRUEFALSE 是布尔值。我找到了错误的变量。
  • 你也是!再次感谢,我很高兴我学到了一个新的“调试”代码print (df.dtypes),很确定我以后会经常使用它,因为很难记住每个变量的类型。
猜你喜欢
  • 2017-02-07
  • 1970-01-01
  • 2019-12-14
  • 2019-03-07
  • 2021-11-17
  • 2018-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多