How can a Pandas DataFrame row be converted into a list? Can you provide an example?

How can a Pandas DataFrame row be converted into a list? Can you provide an example?

A Pandas DataFrame is a two-dimensional data structure in Python used for data manipulation and analysis. It consists of rows and columns, and each row represents a different observation or record. To convert a Pandas DataFrame row into a list, the “tolist()” method can be used. This method converts the values in the row into a list format and returns it. An example of this would be:

df_row = pd.DataFrame({‘col1′:[1,2,3],’col2’:[4,5,6]})
row_list = df_row.iloc[0].tolist()
print(row_list)

This would output [1, 4], as the first row in the DataFrame has values 1 and 4 in the columns “col1” and “col2” respectively. By using the “tolist()” method, the row is converted into a list containing these two values.

Convert Pandas DataFrame Row to List (With Example)


You can use the following basic syntax to convert a row in a pandas DataFrame to a list:

row_list = df.loc[2, :].values.flatten().tolist()

This particular syntax converts the values in row index position 2 of the DataFrame into a list.

The following example shows how to use this syntax in practice.

Example: Convert Pandas DataFrame Row to List

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    B      22        7         8
2    C      19        7        10
3    D      14        9         6
4    E      14       12         6
5    F      11        9         5
6    G      20        9         9
7    H      28        4        12

We can use the following syntax to convert the values in row index position 2 to a list:

#convert row at index 2 to list
row_list = df.loc[2, :].values.flatten().tolist()

#view results
print(row_list)

['C', 19, 7, 10]

We can see that the values in row index position 2 have been converted to a list with four values.

We can confirm that the result is indeed a list by using the type() function:

#view type
print(type(row_list))

<class 'list'>

If you only want the values from specific columns to be included in the list, you can specify the columns by name.

For example, we can use the following syntax to convert the values in row index position 2 to a list for the team and points columns only:

#convert values in row index position 2 to list (for team and points columns)
row_list = df.loc[2, ['team', 'points']].values.flatten().tolist()

#view results
print(row_list)

['C', 19]

Notice that only the values in the team and points columns have been included in the list.

Cite this article

stats writer (2024). How can a Pandas DataFrame row be converted into a list? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-pandas-dataframe-row-be-converted-into-a-list-can-you-provide-an-example/

stats writer. "How can a Pandas DataFrame row be converted into a list? Can you provide an example?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-a-pandas-dataframe-row-be-converted-into-a-list-can-you-provide-an-example/.

stats writer. "How can a Pandas DataFrame row be converted into a list? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-pandas-dataframe-row-be-converted-into-a-list-can-you-provide-an-example/.

stats writer (2024) 'How can a Pandas DataFrame row be converted into a list? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-pandas-dataframe-row-be-converted-into-a-list-can-you-provide-an-example/.

[1] stats writer, "How can a Pandas DataFrame row be converted into a list? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can a Pandas DataFrame row be converted into a list? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top