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