我正在尝试创建多个具有相似名称的数据框。名称根据列表更改,并且也加入一个操作。
corr_C=train[train_C].apply(lambda x: x.corr(train['target'])).abs()
corr_C=corr_C.sort_values(ascending=False, inplace=True)
我有train_D,train_E和train_F的数据集,我想对这些数据集应用相同的功能。
可用的在线解决方案仅讨论跨列的循环,但是我需要可更改的功能来创建新的数据框。
list=['C','D','E','F']
for list in list:
corr_+list=train[train_list].apply(lambda x: x.corr(train['target'])).abs()
return corr_+list=corr_list.sort_values(ascending=False, inplace=True)
SyntaxError:语法无效
答案 0 :(得分:0)
您可以制作一个字典,然后用键(名称)和值(数据框)填充。这是我通常要做的。
#use pandas for dataframe and numpy for random
import pandas as pd
import numpy as np
#some random array data to turn into pd dataframes
my_arrays = []
for i in range(0, 3):
my_arrays.append(np.random.randint(10, size=(5,5)))
#some array names (this could be done more programmatically)
my_array_names = ["First", "Second", "Third"]
#make a dictionary
d = {}
for i in range(0, len(my_arrays)):
#Populate dictionary --> d[key]=value
d[my_array_names[i]]=pd.DataFrame(my_arrays[i], columns = ['C1', 'C2', 'C3', 'C4', 'C5'], index = ['R1', 'R2', 'R3', 'R4', 'R5'])
#print them out to take a look
for key, value in d.items():
print(key)
print(value)
#or call individually
#print(d["First"])
答案 1 :(得分:0)
我最终对其进行了整理:
col={'C','D','E','F'}
for col in col:
dfname=('corr_'+col)
dfnew=train['train'+col].apply(lambda x: x.corr(train['target'])).abs()
locals() [dfname]=dfnew