我正在尝试返回一个数据框,其中行和列都相反,
这是我的代码:
my_names = [2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
cols = ['link', 'post','shared','timeline','status']
ser = [nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen]
df = pd.concat(ser, axis=1, keys=my_names)
我试图给我们pd.pivot做到这一点:
df1 = df.pivot(index=df.columns, columns=df.index, values=df.values)
但这是我收到的回溯信息:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-23-c08b1071deea> in <module>()
----> 1 df1 = df.pivot(index=df.columns, columns=df.index, values=df.values)
/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in pivot(self, index, columns, values)
4380 """
4381 from pandas.core.reshape.reshape import pivot
-> 4382 return pivot(self, index=index, columns=columns, values=values)
4383
4384 _shared_docs['pivot_table'] = """
/usr/local/lib/python3.6/dist-packages/pandas/core/reshape/reshape.py in pivot(self, index, columns, values)
385 else:
386 index = self[index]
--> 387 indexed = Series(self[values].values,
388 index=MultiIndex.from_arrays([index, self[columns]]))
389 return indexed.unstack(columns)
/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in __getitem__(self, key)
2131 if isinstance(key, (Series, np.ndarray, Index, list)):
2132 # either boolean or fancy integer index
-> 2133 return self._getitem_array(key)
2134 elif isinstance(key, DataFrame):
2135 return self._getitem_frame(key)
/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in _getitem_array(self, key)
2175 return self._take(indexer, axis=0, convert=False)
2176 else:
-> 2177 indexer = self.loc._convert_to_indexer(key, axis=1)
2178 return self._take(indexer, axis=1, convert=True)
2179
/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
1267 if mask.any():
1268 raise KeyError('{mask} not in index'
-> 1269 .format(mask=objarr[mask]))
1270
1271 return _values_from_object(indexer)
KeyError: '[(6.0606060606060606, 1.6666666666666667, 23.88535031847134, 18.660287081339714, 3.943661971830986, 16.666666666666664, 52.96703296703297, 72.85067873303167, 80.63380281690141, 55.69620253164557)\n (8.080808080808081, 15.833333333333332, 0.0, 0.0, 5.352112676056338, 10.256410256410255, 2.857142857142857, 9.049773755656108, 9.507042253521126, 21.518987341772153)\n (0.0, 1.6666666666666667, 0.3184713375796179, 3.588516746411483, 13.239436619718308, 11.282051282051283, 5.4945054945054945, 5.429864253393665, 2.464788732394366, 5.063291139240507)\n (85.85858585858585, 71.66666666666667, 59.87261146496815, 65.55023923444976, 67.6056338028169, 43.84615384615385, 29.010989010989015, 9.95475113122172, 6.338028169014084, 16.455696202531644)\n (0.0, 9.166666666666666, 15.92356687898089, 12.200956937799043, 9.859154929577464, 17.94871794871795, 9.670329670329672, 2.7149321266968327, 1.056338028169014, 1.2658227848101267)] not in index'
答案 0 :(得分:1)
怎么样:
df.transpose()
我认为应该这样做。试试看。
顺便说一句,您生成数据框的代码有很多错误,或者我使用的方式错误。
答案 1 :(得分:1)
答案 2 :(得分:1)
创建工作区
import random
import pandas as pd
# your variables
my_names = [2009,2010,2011,2012,2013,2014,2015,2016,2017,2018]
cols = ['link', 'post','shared','timeline','status']
# since we don't have the list of variables in ser, I'm creating it and creating a list of lists of values
ser = []
for x in range(0,len(cols)):
a = [random.uniform(0,1) for _ in range(len(my_names))]
ser.append(a)
# also swapped the pd.concat to create a dataframe as your image-example shows
df = pd.DataFrame(ser, columns=my_names, index = cols)
解决方案:
# a simple transpose will do the trick for what you need:
df.T