Pandas Series & # 8594; Dataframe Create columns from rows and fill numbering with zeros

I have a pandas framework with multiple columns. Then I pull out df [reason] and get a series

some text 1 0.1178 Other string 0.0732 another string 0.0534 Name: reason, dtype: float64 

What I would like to convert to this is a dataframe for what looks like this:

  some text 1 Other string another string some text 1 0.1178 0 0 Other string text 0 0.0732 0 another string here 0 0 0.0534 

How to duplicate rows as columns and fill in inappropriate rows / columns with 0?

+4
source share
1 answer

You can use np.diag :

 >>> ts some text 1 0.1178 Other string 0.0732 another string 0.0534 dtype: float64 >>> DataFrame(np.diag(ts), columns=ts.index, index=ts.index) some text 1 Other string another string some text 1 0.1178 0.0000 0.0000 Other string 0.0000 0.0732 0.0000 another string 0.0000 0.0000 0.0534 
+5
source

All Articles