How to Convert Row Index to List of Dataframe in Python

To convert a row index to a list in a dataframe in Python, you can use the values() and tolist() functions.

The following example shows how to convert a row index to a list in a dataframe using the values() and tolist() functions in Python.

Using values() & tolist() Function

We can use the values() and tolist() functions to convert a row index to a list.

Suppose we have the following dataframe:

# Import pandas library
import pandas as pd

# Create the DataFrame
office_stuff = pd.DataFrame({
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]
})

# Convert a row index to a list
row_index_list = office_stuff.iloc[1].values.tolist()

# Print the row index list
print(row_index_list)

Output: 👇️

['01-03-2023', 'A-102', 'Mobile', 550, 1]

In this example, we use the iloc function to select the row at index 1, then use the values() function to get the values of the row, and finally use the tolist() function to convert these values into a list.

Conclusion

We can use the values() and tolist() functions to convert a row index to a list in a dataframe in Python. This method provides a convenient way to extract row data as a list from a dataframe.