在Python中返回数据帧的多个列的多个值

2022-01-30 15:08:13 标签 pythonpandasdataframe

我有这样的代码

def function1(idData):
  movie = movdb.Movies(idData)
  response = movie.info()  
  runtime = movie.runtime 
  return runtime
def function2(idData):
  movie = movdb.Movies(idData)
  response = movie.info()  
  lang = movie.original_language 
  return lang
def function2(idData):
  movie = movdb.Movies(idData)
  response = movie.info()
  overview = movie.overview
  return overview
def main():
  df['Column 1'] = df.apply(lambda row : function1(row['Id'], axis = 1)
  df['Column 2'] = df.apply(lambda row : function2(row['Id']), axis = 1)
  df['Column 3'] = df.apply(lambda row : function3(row['Id']), axis = 1)
  
if __name__ == '__main__':
  main()

这很有效,但我觉得效率不高。有没有办法返回多个值到多个列?所以我只需要访问下面的代码一次。

movie = tmdb.Movies(idData)
response = movie.info()

或者你有什么想法使这段代码更有效?

# # #

def function(idData):
  movie = movdb.Movies(idData)
  response = movie.info()  
  runtime = movie.runtime 
  lang = movie.original_language 
  overview = movie.overview
  return [[runtime,lang,overview]]
def main():
  df[['Column 1','Column 2','Column 3']] = df.apply(lambda row : function1(row['Id'], axis = 1)
if __name__ == '__main__':
  main()
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码