python - 带有 for 循环和函数调用的函数
问题描述
我有一个功能如下:
def get_type(df, index):
df_type = df[index] #so i can obtain for index 0, 1, 2.. until n-1
return df_type
我想在一个循环遍历索引并将它们附加到列表中的函数中创建一个 for 循环,例如:
def all_df_list(df, index):
#where index is n-1
list_of_df = []
list_of_columns = []
for i in range(index):
df = get_type(df, i)
list_of_df.append(df)
list_of_df_colums(df.columns[0])
这样我就可以获得所有索引 df 及其列的列表:
get_df_and_columns = all_df_list(df, index=50)
#say we have 51 items in the df
我该如何处理这样的事情?
解决方案
假设您有一个数据框 df 并希望它的索引和列作为列表
def all_df_list(df):
col = df.columns.to_list()
index = df.index.to_list()
return (col, index)
推荐阅读
- blockchain - 调用智能合约上的函数以将 trx 发送到地址时出现错误 502
- docker - shell 脚本(busybox)在 docker 容器上不能按预期工作
- asp.net - 隐藏/排除控制器
- python - 如何在 Python 中从数组中的字符串中分隔字母和数字?
- grafana - 针对 AWS Cloudwatch 的 Grafana 注释查询
- r - R中的区域虚拟变量
- xslt - 带有子字符串的应用模板
- azure-data-factory - 我们如何定义一个可重用的错误处理程序活动 Web 活动以在 ADF 管道中的任何位置发生故障时发送电子邮件?
- spring - spring 基于构造函数的依赖注入,如何理解“它可以让你将应用程序组件实现为不可变对象”
- sql - 在 SQL 中查询另一列中的 JSON 对象时如何使用 where 子句引用一列