Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pandas-在数据帧中创建新列时if函数出错_Python_Pandas - Fatal编程技术网

Python Pandas-在数据帧中创建新列时if函数出错

Python Pandas-在数据帧中创建新列时if函数出错,python,pandas,Python,Pandas,我在df草稿中有一列lastsquare类型对象(无NULL或NaN)。我想创建一个新的列Age\u retired,该列基于lastseasy最后两位数字与50的比较 这是上一季的专栏 0 1993-94 1 1990-91 2 1993-94 3 1997-98 Name: lastseason, dtype: object 提取最后2位数字并转换为数字 print pd.to_numeric(draft['lastseason'].asty

我在df
草稿
中有一列
lastsquare
类型对象(无NULL或NaN)。我想创建一个新的列
Age\u retired
,该列基于
lastseasy
最后两位数字与50的比较

这是上一季的专栏

0       1993-94
1       1990-91
2       1993-94
3       1997-98
Name: lastseason, dtype: object
提取最后2位数字并转换为数字

print pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='coerce')
0       94
1       91
2       94
3       98
Name: lastseason, dtype: int64
创建列
Age\u retired

if pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='coerce') <50:
 draft['Age_retired'] = 2000 + pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='coerce')
else:
 draft['Age_retired'] = 1900 + pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='coerce')

if pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='胁迫')Yes,if-else构造将不会按元素进行计算。但是,使用series
.map
方法可以很容易地解决这个问题,该方法应用了一个函数元素。首先定义函数,然后映射它。您只需将映射结果分配到
draft['age\u retired']
即可创建新列

In [10]: def add_age_retired(x):
             if x < 50:
                 return 2000 + x
             else:
                 return 1900 + x


In [11]: pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='coerce').map(add_age_retired)
Out[11]: 
0    1994
1    1991
2    1994
3    1998
Name: lastseason, dtype: int64

In [12]: draft['Age_retired'] = pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='coerce').map(add_age_retired)

In [13]: draft
Out[13]: 
  lastseason  Age_retired
0    1993-94         1994
1    1990-91         1991
2    1993-94         1994
3    1997-98         1998
[10]中的
def add_age_retired(x):
如果x<50:
返回2000+x
其他:
返回1900+x
在[11]中:pd.to_numeric(草稿['lastseason'].astype('str').str[-2:],errors='胁迫').map(添加退休年龄)
出[11]:
0    1994
1    1991
2    1994
3    1998
姓名:lastseason,数据类型:int64
在[12]中:draft['Age\u retired']=pd.to\u numeric(draft['lastsearch'].astype('str').str[-2:],errors='concurve').map(添加退休年龄)
在[13]中:草稿
出[13]:
上赛季退休
0    1993-94         1994
1    1990-91         1991
2    1993-94         1994
3    1997-98         1998

是的,if-else构造不会按元素计算。但是,使用series
.map
方法可以很容易地解决这个问题,该方法应用了一个函数元素。首先定义函数,然后映射它。您只需将映射结果分配到
draft['age\u retired']
即可创建新列

In [10]: def add_age_retired(x):
             if x < 50:
                 return 2000 + x
             else:
                 return 1900 + x


In [11]: pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='coerce').map(add_age_retired)
Out[11]: 
0    1994
1    1991
2    1994
3    1998
Name: lastseason, dtype: int64

In [12]: draft['Age_retired'] = pd.to_numeric(draft['lastseason'].astype('str').str[-2:],errors='coerce').map(add_age_retired)

In [13]: draft
Out[13]: 
  lastseason  Age_retired
0    1993-94         1994
1    1990-91         1991
2    1993-94         1994
3    1997-98         1998
[10]中的
def add_age_retired(x):
如果x<50:
返回2000+x
其他:
返回1900+x
在[11]中:pd.to_numeric(草稿['lastseason'].astype('str').str[-2:],errors='胁迫').map(添加退休年龄)
出[11]:
0    1994
1    1991
2    1994
3    1998
姓名:lastseason,数据类型:int64
在[12]中:draft['Age\u retired']=pd.to\u numeric(draft['lastsearch'].astype('str').str[-2:],errors='concurve').map(添加退休年龄)
在[13]中:草稿
出[13]:
上赛季退休
0    1993-94         1994
1    1990-91         1991
2    1993-94         1994
3    1997-98         1998

谢谢@juanpa.arrivillaga。好奇的是,在不定义新函数的情况下,是否还有其他方法可以做同样的事情?@Square9627如果不想定义新函数,可以使用lambda函数:
…map(lambda x:2000+x如果<50 else 1900+x)
谢谢@juanpa.arrivillaga。好奇的是,在不定义新函数的情况下,是否还有其他方法可以做同样的事情?@Square9627如果不想定义新函数,可以使用lambda函数:
…map(lambda x:2000+x if<50 else 1900+x)