use the to_frame
method, then transpose with T
df = pd.DataFrame([[100,200,300],[400,500,600]]) for index, row in df.iterrows(): print(row.to_frame().T) 0 1 2 0 100 200 300 0 1 2 1 400 500 600
Note:
This is similar to @JohnE's answer in that the to_frame
method is the syntactic sugar around pd.DataFrame
.
In fact, if we follow the code
def to_frame(self, name=None): """ Convert Series to DataFrame Parameters ---------- name : object, default None The passed name should substitute for the series name (if it has one). Returns ------- data_frame : DataFrame """ if name is None: df = self._constructor_expanddim(self) else: df = self._constructor_expanddim({name: self}) return df
Points to _constructor_expanddim
@property def _constructor_expanddim(self): from pandas.core.frame import DataFrame return DataFrame
What you see is just returning a called DataFrame
source share