Site icon Amelt.net

Python:How to convert each row of pandas dataframe to tuple

Amelt

Amelt

This post is also available in: 日本語 (Japanese)

This is a note about how to convert each row of pandas dataframe to tuple.

import pandas as pd
import numpy as np

#Create dummy dataframe
df = pd.DataFrame(np.arange(9).reshape(3, 3))

print(df)
"""
#output
   0  1  2
0  0  1  2
1  3  4  5
2  6  7  8
"""

_tuple =  [tuple(x) for x in df.values]
print(_tuple)
"""
#output
[(0, 1, 2), (3, 4, 5), (6, 7, 8)]
"""