【发布时间】:2020-07-16 23:50:56
【问题描述】:
事情是这样的,我需要将来自其他数据框的一行放在 pandas 中主数据框的顶部,在第一行的上方,其中列名。
示例:
1value 2value 3value 4value 5value
acity 4 3 6 2 6
bcity 2 6 6 4 1
ccity 5 11 53 6 3
dcity 5 1 4 6 3
gcity 6 4 2 7 4
还有另一个示例:
1value 2value 3value 4value 5value
2 5 2 6 3
现在我需要将第二个样本的值添加到第一个样本的顶部。期望的输出:
2 5 2 6 3
1value 2value 3value 4value 5value
acity 4 3 6 2 6
bcity 2 6 6 4 1
ccity 5 11 53 6 3
dcity 5 1 4 6 3
gcity 6 4 2 7 4
顺便提一下,我在这个示例数据框中有大约 3000 行和 250 列。
我尝试过使用多索引,但不起作用,它说:TypeError: unhashable type: 'dict'
TypeErrorTraceback
(most recent call last)
<ipython-input-230-e17cd26b6b7f> in <module>()
----> 1 pd.MultiIndex.from_arrays([newdf.columns.to_series().map(topframe.iloc[0].to_dict()), newdf.columns])
/opt/anaconda2/lib/python2.7/site-packages/pandas/indexes/multi.pyc in from_arrays(cls, arrays, sortorder, names)
841 return Index(arrays[0], name=name)
842
--> 843 cats = [Categorical.from_array(arr, ordered=True) for arr in arrays]
844 levels = [c.categories for c in cats]
845 labels = [c.codes for c in cats]
/opt/anaconda2/lib/python2.7/site-packages/pandas/core/categorical.pyc in from_array(cls, data, **kwargs)
385 the unique values of `data`.
386 """
--> 387 return Categorical(data, **kwargs)
388
389 @classmethod
/opt/anaconda2/lib/python2.7/site-packages/pandas/core/categorical.pyc in __init__(self, values, categories, ordered, name, fastpath, levels)
286 codes, categories = factorize(values, sort=True)
287 except TypeError:
--> 288 codes, categories = factorize(values, sort=False)
289 if ordered:
290 # raise, as we don't have a sortable data structure and so
/opt/anaconda2/lib/python2.7/site-packages/pandas/core/algorithms.pyc in factorize(values, sort, order, na_sentinel, size_hint)
183 table = hash_klass(size_hint or len(vals))
184 uniques = vec_klass()
--> 185 labels = table.get_labels(vals, uniques, 0, na_sentinel, True)
186
187 labels = com._ensure_platform_int(labels)
pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_labels (pandas/hashtable.c:14033)()
TypeError: unhashable type 'dict
【问题讨论】:
-
请发布完整的回溯,以便更容易找到错误的实际位置。
-
我已经编辑了@NickilMaveli