python创建DataFrame,并实现DataFrame的转置

>>> import pandas as pd
>>> import numpy as np
>>> x1 = {1: 106, 2: 3, 7: 42}
>>> a = x1.keys()
>>> b = x1.values()
>>> df = pd.DataFrame([a,b],index=['type', 'cnt'])#创建dataframe
>>> df2 = pd.DataFrame(df.values.T, index=df.columns, columns=df.index)#转置
>>> print(df)
        0  1   2
type    1  2   7
cnt   106  3  42
>>> print(df2)
   type  cnt
0     1  106
1     2    3
2     7   42
>>> 

  

posted @ 2019-11-08 15:26  牛郎  阅读(26939)  评论(0编辑  收藏  举报