Python 带多个参数的pands.apply()函数

Python 带多个参数的pands.apply()函数,python,pandas,dataframe,apply,Python,Pandas,Dataframe,Apply,df=岩石文件 问题:编写一个函数,它将获取一行数据帧并打印出歌曲、艺术家以及发布日期是否小于1970年 定义我的功能: def release_info(row): """Checks if song is released before or after 1970.""" if rocksfile.loc[row, 'Release_Year'] < 1970: print str(rocksfile.loc[row,'Song_Clean']) + "

df=岩石文件

问题:编写一个函数,它将获取一行数据帧并打印出歌曲、艺术家以及发布日期是否小于1970年

定义我的功能:

def release_info(row):
    """Checks if song is released before or after 1970."""
    if rocksfile.loc[row, 'Release_Year'] < 1970:
        print str(rocksfile.loc[row,'Song_Clean']) + " by " + 
str(rocksfile.loc[row,'Artist_Clean']) \
            + " was released before 1970."
    else:
        print str(rocksfile.loc[row,'Song_Clean']) + " by " + str(rocksfile.loc[row,'Artist_Clean']) \
            + " was released after 1970."
错误消息:

TypeError                                 Traceback (most recent call last)
<ipython-input-61-fe0405b4d1e8> in <module>()
  1 #a = [1]
  2 
----> 3 rocksfile.apply(release_info, axis = 1, row=1)


TypeError: ("release_info() got multiple values for keyword argument 'row'", u'occurred at index 0')
TypeError回溯(最近一次调用)
在()
1#a=[1]
2.
---->3岩石文件应用(发布信息,轴=1,行=1)
TypeError:(“release_info()为关键字参数“row”获取了多个值,u”出现在索引0处)

在熊猫使用
数组
s(
系列
数据帧
)时释放信息(1)

,因此更好地使用矢量化
熊猫
numpy
函数,这里最好使用:

#条件
m=岩石文件【发布年份】<1970年
#将列连接在一起
a=摇滚乐['Song_Clean']+“by”+rocksfile['Artister_Clean']
#在末尾添加不同的字符串
b=a+“在1970年之前发布。”
c=a+“在1970年后发布。”
岩石文件['new']=np.式中(m,a,b)
打印(rocksfile)

您可以使用
np.where
并将其减少到1行

s = rocksfile['Song_Clean'] 
    + ' was released by ' 
    + rocksfile['Artist_Clean'] 
    + pd.Series(np.where(rocksfile['Release_Year'] < 1970, 'before', 'after'))
    + ' 1970'

rocksfile['new'] = s
s=rocksfile['Song_Clean']
+“已由发布”
+rocksfile['Artister_Clean']
+pd.系列(np.式中(rocksfile['Release_Year']<1970,'before','before'))
+ ' 1970'
岩石文件['new']=s
此处:

rocksfile.apply(release_info, axis = 1, row=1)
不是
数据帧的一部分。apply()
预期参数,因此对于
release\u info()
除了第一个位置参数,因此
release\u info()
最终被这样调用:

release_info(row_index, row=1)
rocksfile.apply(release_info, axis = 1, row=1)
release_info(row_index, row=1)