Python pandas:while循环可通过多个列表和调用函数同时前进
我希望我的代码:Python pandas:while循环可通过多个列表和调用函数同时前进,python,python-3.x,pandas,while-loop,Python,Python 3.x,Pandas,While Loop,我希望我的代码: 从CSV中读取数据并制作数据帧:“source_df” 查看dataframe是否包含列表中指定的任何列: “可能的_列” 调用一个唯一的函数来替换列表中“可能的_列”中标题所在的每列中的值,然后将修改后的值插入新的数据框:“destination_df” 这是: import pandas as pd #creates source_df file = "yes-no-true-false.csv" data = pd.read_csv(file)
- 从CSV中读取数据并制作数据帧:“source_df”
- 查看dataframe是否包含列表中指定的任何列: “可能的_列”
- 调用一个唯一的函数来替换列表中“可能的_列”中标题所在的每列中的值,然后将修改后的值插入新的数据框:“destination_df”
import pandas as pd
#creates source_df
file = "yes-no-true-false.csv"
data = pd.read_csv(file)
source_df = pd.DataFrame(data)
#creates destination_df
blanklist = []
destination_df = pd.DataFrame(blanklist)
#create the column header lists for comparison in the while loop
columns = source_df.head(0)
possible_columns = ['yes/no','true/false']
#establish the functions list and define the functions to replace column values
fix_functions_list = ['yes_no_fix()','true_false_fix()']
def yes_no_fix():
destination_df['yes/no'] = destination_df['yes/no fixed'].replace("No","0").replace("Yes","1")
def true_false_fix():
destination_df['true/false'] = destination_df['true/false fixed'].replace('False', '1').replace('True', '0')
'''use the counter to call a unique function from the function list to replace the values in each column whose header is found in the "possible_columns" the list, insert the modified values in "destination_df, then advance the counter'''
counter = 0
while counter < len(possible_columns):
if possible_columns[counter] in columns:
destination_df.insert(counter, possible_columns[counter], source_df[possible_columns[counter]])
fix_functions_list[counter]
counter = counter + 1
#see if it works
print(destination_df.head(10))
将熊猫作为pd导入
#创建源文件
file=“yes no true false.csv”
数据=pd.read\u csv(文件)
source_df=pd.DataFrame(数据)
#创建目的地
空白列表=[]
目的地_df=pd.DataFrame(空白列表)
#创建列标题列表,以便在while循环中进行比较
columns=源文件头(0)
可能的_列=['yes/no','true/false']
#建立函数列表并定义替换列值的函数
fix_函数\u列表=[“是\u否\u fix()”,“真\u假\u fix()”]
def yes_no_fix():
目的地_-df['yes/no']=目的地_-df['yes/no-fixed']。替换(“no”,“0”)。替换(“yes”,“1”)
def true_false_fix():
目的地_-df['true/false']=目的地_-df['true/false-fixed'].替换('false','1')。替换('true','0'))
''使用计数器从函数列表中调用唯一函数,替换列表中“可能的_列”中标题所在的每列中的值,在“目标_df”中插入修改后的值,然后推进计数器''
计数器=0
当计数器
当我打印(destination_df)时,我看到来自source_df的未修改的列值。当我独立调用函数时,它们工作,这让我觉得while循环中出现了问题。您的问题是,您试图调用一个以字符串形式存储在列表中的函数
fix_functions_list[cnt]
这实际上不会运行函数,只会访问字符串值。
我会尝试找到另一种方法来运行这些函数。您的问题是,您试图调用一个以字符串形式存储在列表中的函数
fix_functions_list[cnt]
def yes_no_fix():
destination_df['yes/no'] = destination_df['yes/no fixed'].replace("No","0").replace("Yes","1")
def true_false_fix():
destination_df['true/false'] = destination_df['true/false fixed'].replace('False', '1').replace('True', '0')
fix_functions_list = {0:yes_no_fix,1:true_false_fix}
这实际上不会运行函数,只会访问字符串值。
我会尝试找到另一种方式来运行这些函数
def yes_no_fix():
destination_df['yes/no'] = destination_df['yes/no fixed'].replace("No","0").replace("Yes","1")
def true_false_fix():
destination_df['true/false'] = destination_df['true/false fixed'].replace('False', '1').replace('True', '0')
fix_functions_list = {0:yes_no_fix,1:true_false_fix}
并将函数调用更改为如下所示
fix_functions_list[counter]()
并将函数调用更改为如下所示
fix_functions_list[counter]()
这些正是我所希望的简单编辑类型。然而,当我尝试它们时,我得到一个“KeyError:3”,它表示修复函数列表[计数器]().Thinks?从if条件内部移动计数器并在if条件之后添加您所做的是,即使在没有匹配的情况下,您也在增加计数器。请查看下面的答案,如果您有任何错误,请告诉我。如果运行时没有错误,您将得到所需的答案。当我将计数器更新移动到if条件外部时,我仍然得到了KeyError这正是我所希望的简单编辑类型。然而,当我尝试它们时,我得到了一个“KeyError:3”,表示修复函数列表[计数器]().Thinks?从if条件内部移动计数器并在if条件之后添加您所做的是,即使在没有匹配的情况下,您也在增加计数器。请查看下面的答案,如果您有任何错误,请告诉我。如果运行时没有错误,您将得到所需的答案。当我将计数器更新移动到if条件外部时,我仍然得到KeyErrorThank you for this example。但是,我发布的代码是一个简化的示例。实际上,我将使用的数据集将有25+列,每个列最多有15个变量,因此映射的想法似乎很难编写。感谢您提供的这个示例。但是,我发布的代码是一个简化的exampl实际上,我将使用的数据集将有25+列,每个列最多有15个变量,因此映射的想法似乎很难编写。