从数据框列表中合并for循环中的数据框

时间:2019-05-24 09:58:51

标签: python pandas dataframe

我有一个pd.dataframes列表,想将它们分别与另一个数据帧合并,以便获得几个数据帧作为输出。我尝试将它们合并并保存到字典中,但是收到一个错误,表明我的列表无法散列。

import pandas as pd
import numpy as np




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:
    for direction in directions:
        merges.append(pd.merge(location, direction, on=['time'], how=['inner'])) 



x=0
y=0.5
for Turbine in merges:

    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)

我不知道为什么列表无法散列。错误发生在合并数据帧的for循环中(merges.append .....) TypeError:无法散列的类型:“列表”

这是完整的输出:


  File "C:\Users\Elias\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\Elias\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Elias/Anaconda3/Scripts/Masterarbeit/efficiency_curves.py", line 35, in <module>
    merges.append(pd.merge(location, direction, on=['time'], how=['inner']))

  File "C:\Users\Elias\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 62, in merge
    return op.get_result()

  File "C:\Users\Elias\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 568, in get_result
    join_index, left_indexer, right_indexer = self._get_join_info()

  File "C:\Users\Elias\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 777, in _get_join_info
    right_indexer) = self._get_join_indexers()

  File "C:\Users\Elias\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 756, in _get_join_indexers
    how=self.how)

  File "C:\Users\Elias\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py", line 1146, in _get_join_indexers
    join_func = _join_functions[how]

TypeError: unhashable type: 'list'

1 个答案:

答案 0 :(得分:1)

merge中,how应该是字符串而不是列表。您必须写:

    merges.append(pd.merge(location, direction, on=['time'], how='inner'))