我想将对称相似性矩阵(pd.DataFrame)转换为pd.Series带a的未堆叠状态,pd.MultiIndex然后再转换为apd.DataFrame
这是获得对称的代码,pd.DataFrame也是我尝试反转操作的尝试。
我应该使用pivot吗?我想以另一个pd.DataFrame名称结尾df_sqr_revert,该名称与原始名称相同df_sqr。
有谁知道如何撤销此操作?
data = {'sepal_length': {'sepal_length': 1.0, 'sepal_width': 0.44531537502467533, 'petal_length': 0.935877078652436, 'petal_width': 0.9089768166845817}, 'sepal_width': {'sepal_length': 0.44531537502467533, 'sepal_width': 1.0, 'petal_length': 0.2897419517994226, 'petal_width': 0.32172795519309727}, 'petal_length': {'sepal_length': 0.935877078652436, 'sepal_width': 0.2897419517994226, 'petal_length': 1.0, 'petal_width': 0.9813785485254833}, 'petal_width': {'sepal_length': 0.9089768166845817, 'sepal_width': 0.32172795519309727, 'petal_length': 0.9813785485254833, 'petal_width': 1.0}}
df_sqr = pd.DataFrame(data)
# petal_length petal_width sepal_length sepal_width
# petal_length 1.000000 0.981379 0.935877 0.289742
# petal_width 0.981379 1.000000 0.908977 0.321728
# sepal_length 0.935877 0.908977 1.000000 0.445315
# sepal_width 0.289742 0.321728 0.445315 1.000000
Se_vertical = df_sqr.unstack()
# petal_length petal_length 1.000000
# petal_width 0.981379
# sepal_length 0.935877
# sepal_width 0.289742
# petal_width petal_length 0.981379
# petal_width 1.000000
# sepal_length 0.908977
# sepal_width 0.321728
# sepal_length petal_length 0.935877
# petal_width 0.908977
# sepal_length 1.000000
# sepal_width 0.445315
# sepal_width petal_length 0.289742
# petal_width 0.321728
# sepal_length 0.445315
# sepal_width 1.000000
# dtype: float64
# df_sqr_revert = Se_vertical.stack()
# AttributeError: 'Series' object has no attribute 'stack'
LEATH
相关分类