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
>>> 

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM