python - 返回 2 个或更多 dfs 的函数 - 如何在 main() 函数中调用它?
问题描述
我有一系列函数,然后我想在 main() 函数中调用它们。我已经能够做到这一点,除了我不确定如何处理返回两个或更多数据帧的函数。你如何在 main() 函数中调用这个函数?谢谢您的帮助!
我的猜测是这样的:
df, df2 = operation_one(df, df2)
import pandas as pd
import numpy as np
def loader():
df = pd.read_excel('file_example.xlsx')
return df
def clean_data(df):
del df['column_7']
return df
def operation_one(df):
del df['column_12']
df2 = pd.DataFrame({'Color': ['Blue', 'Yellow'], 'Size': ['big', 'small']})
return df, df2
def main():
df = loader()
df = clean_data(df)
df = operation_one(df)
df, df2 = operation_one(df, df2)
with pd.ExcelWriter("file.xlsx") as writer:
df.to_excel(writer, sheet_name='first' , index=False)
df2.to_excel(writer, sheet_name='second' , index=False)
if __name__ == "__main__":
main()
解决方案
问题不在于分配返回值,而在于传递给函数的参数 df2 从未分配过。我假设更远的“df =”分配之一应该是“df2 =”。
推荐阅读
- python - 当我尝试在 Python 中解析 JSON 时,“TypeError:列表索引必须是整数或切片,而不是 str”
- elasticsearch - Kibana Tophits 通过一个字段而不是所有字段转换组
- python - 如何在 whoosh 中返回与我们的搜索关键字匹配的对应行?
- lua - 如何在不使用 Lua 脚本中定义的任何函数的情况下运行函数
- node.js - 删除打字稿界面中不存在的属性
- email - 为什么我的自定义 postfix 虚拟邮箱不起作用
- python - 从模型中获取类数组
- node.js - 如何在 Mongoose 中使用填充查询值?
- java - 使用 Xstream 反序列化时验证 XML 的内容
- reactjs - 重新启动计算机后,当我再次为 React 项目运行“npm start”时,它显示以下错误!帮帮我