为for循环的每次迭代保存一个新的数据框

时间:2019-05-23 14:29:01

标签: python pandas

我使用for循环创建多个pd.Dataframe,但是我无法弄清楚如何为每个循环单独保存每个。提供的代码只是我所做的工作的最低限度,但是我认为它足以解决这个问题。我想为每个方向分别保存一个数据框。到目前为止,我总是陷入最后一个,而其他人则迷路了。列表“路线”中的所有项目都是它们自己的数据框。

我尝试计算每个迭代步骤,并向数据框名称添加+1,但未按计划进行。理想的做法是添加方向的名称(N,NE等)

这里是我现在走了多远,还有更多的代码:

turbine         = pd.read_csv('testdaten.csv', sep=';')
turbine.columns = ['time', 'speed_turbine', 'degree_turbine', 'direction_turbine']
Emden           = pd.read_csv('rose.csv', sep=';')
Emden.columns   = ['time', 'speed_data', 'degree_data', 'direction_data']

N               = Emden.loc[(Emden['direction_data'] == 'N')]
NE              = Emden.loc[(Emden['direction_data'] == 'NE')]  
E               = Emden.loc[(Emden['direction_data'] == 'E')]
SE              = Emden.loc[(Emden['direction_data'] == 'SE')]
S               = Emden.loc[(Emden['direction_data'] == 'S')]
SW              = Emden.loc[(Emden['direction_data'] == 'SW')]
W               = Emden.loc[(Emden['direction_data'] == 'W')]
NW              = Emden.loc[(Emden['direction_data'] == 'NW')]

directions = [N, NE, E, SE, S, SW, W, NW]
locations  = [turbine]
merges = []
curves = []

for location in locations:
    d={'df_'+location: pd.merge(location, direction, on=['time'], how=['inner'])for direction in directions}

x=0
y=0.5
for Turbine in d:

    while x <= Turbine['speed_data'].max():
        sub = Turbine.loc[(Turbine['speed_data'] > x)&(Turbine['speed_data'] <= y)]  # filter the dataframe on both conditions
        Turbine.loc[sub.index, str(y)] = Turbine['speed_data']/Turbine['speed_turbine']
        x += .5
        y += .5


    Turbine.loc['Mean_Values'] = Turbine.mean(1)

    curves.append(Turbine)

我收到一个列表不可散列的错误。

2 个答案:

答案 0 :(得分:1)

尝试

directions = [N, NE, E, SE, S, SW, W, NE]
dfs = []

for direction in directions:
    dfs.append(pd.merge(location, direction, on=['time'], how=['inner'])) 

您必须创建一个数据框列表

答案 1 :(得分:0)

您可以使用map

df = dict(map(lambda direction: (direction, pd.merge(location, direction, on=['time'], how=['inner'])), directions))

这将创建一个具有(键,值)对的字典,其中键将是方向,值将是DataFrame