Pandas.DataFrame transpose

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/a19990412/article/details/90744905

Brief

Motivation

  • Sometimes, a way to change the acquired data, can improve the speed of data acquisition.
  • sometimes, crawling anticipation of uncertain length data, so only the first stored.
  • sometimes, there to give you data like this, but can not easily use

In these cases, you might need to encounter method DataFrame ranks transposed.

Contribution

It provides a method of transposition of the ranks Pandas.DataFrame

Experimental part

  • Import Package
>>> import pandas as pd
  • Creating a Data
>>> df = pd.DataFrame([['A', 1, 2], ['B', 3, 4]],  columns=['Name', 'c1', 'c2'])
  • Data reads as follows:
>>> df
  Name  c1  c2
0    A   1   2
1    B   3   4
  • operating:
>>> df2 = pd.DataFrame(df.values.T, index=df.columns, columns=df.index)
>>> df2
      0  1
Name  A  B
c1    1  3
c2    2  4

Conclusion

It is simply a matrix inversion method using numpy built, such operations fastest.

Guess you like

Origin blog.csdn.net/a19990412/article/details/90744905