Categories
Python Answers

How to get list from Python Pandas dataframe column or row?

Spread the love

Sometimes, we want to get list from Python Pandas dataframe column or row.

In this article, we’ll look at how to get list from Python Pandas dataframe column or row.

How to get list from Python Pandas dataframe column or row?

To get list from Python Pandas dataframe column or row, we use the tolist method.

For instance, we write

import pandas as pd

data_dict = {
    "one": pd.Series([1, 2, 3], index=["a", "b", "c"]),
    "two": pd.Series([1, 2, 3, 4], index=["a", "b", "c", "d"]),
}

df = pd.DataFrame(data_dict)
col_one_list = df["one"].tolist()

to create the df data frame from the data_dict dict.

Then we get column one’s values with df['one'] and call tolist to return the values in the series as a list.

Conclusion

To get list from Python Pandas dataframe column or row, we use the tolist method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *