python
Get a column rows as a List in Pandas Dataframe
If you are using Pandas Dataframe and want to get all the rows of a column in a List, you can use the code examples explained in this post.
import pandas as pd
my_data = {
"names": ["Rick", "John", "Carol", "Gibson"],
"profession": ["Engineer", "Carpenter", "Writer", "marketer"]
}
df = pd.DataFrame(my_data)
print(df)
# This will get the list of all the rows in column names
result = df['names'].tolist()
print(result)
Output
names profession
0 Rick Engineer
1 John Carpenter
2 Carol Writer
3 Gibson marketer
['Rick', 'John', 'Carol', 'Gibson']
In the above code example,
1. We have imported the pandas module.
import pandas as pd
2. Created a Pandas Dataframe df from a dictionary.
3. Getting all the row values of the "names" column and converting it to a Python list.
df['names'].tolist()
4. If you want to get all the rows of the "profession" column from the Dataframe, you can use below code for that.
df['profession'].tolist()
Was this helpful?
Similar Posts
- Change column orders using column names list - Pandas Dataframe
- Counting rows in a Pandas Dataframe based on column values
- [Pandas] Add new column to DataFrame based on existing column
- Get column names from Pandas DataFrame as a python List
- Get column values as list in Pandas DataFrame
- Pandas - Change rows order of a DataFrame using index list
- Pandas - Delete multiple rows from DataFrame using index list