问题是,我需要将另一个数据帧中的一行放在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列。你知道吗
我试过使用multiindex,但不起作用,它说:TypeError: unhashable type: 'dict'
类型错误回溯
(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
您可以将第二行的第一行创建的} 一起使用^{} :
dict
与^{另一种解决方案是
rename
列乘以dict
:相关问题 更多 >
编程相关推荐