首页 > 解决方案 > 从数据帧列表中将数据帧合并到 for 循环中

问题描述

我有一个 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:unhashable type:'list'

这是完整的输出:


  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'

标签: pythonpandasdataframe

解决方案


In merge,how应该是一个字符串而不是一个列表。你必须写:

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

推荐阅读