Change language

Create list from strings in Pandas dataframe

|

Solution # 1: To iterate over rows in DataFrame.iterrows () Pandas we can use DataFrame.iterrows () and then add the data of each row to the end of the list.

# import pandas as pd

import pandas as pd

  
# Create data frame

df = pd.DataFrame ({ ’ Date’ : [ ’ 10/2 / 2011’ , ’11/2 / 2011’ , ’12/2 / 2011’ , ’ 13/2 / 11’ ],

’Event’ : [ ’Music’ , ’ Poetry’ , ’Theater’ , ’ Comedy ’ ],

  ’ Cost’ : [ 10000 , 5000 , 15000 , 2000 ]})

 
# Print data frame

print (df)

Output:

 

We will now use DataFrame.iterrows () to iterate over each row of the given Dataframe and building a list from the data of each row.

# Create empty list

Row_list = []

 
# Iterate over each line

for index, rows in df.iterrows ():

# Create a list for the current line

my_list = [rows.Date, rows.Event, rows.Cost]

 

# add list to final list

  Row_list.append (my_list)

 
# Print list

print (Row_list)

Output:

As we can see in the output, we have successfully extracted each row of a given dataframe into a list. Like any other Python list, we can perform any operation on the list of the retrieved list.

# Find the length again
# list created

print ( len (Row_list))

  
# Print first 3 items

print (Row_list [: 3 ])

Output:

Solution # 2. To iterate over the lines in DataFrame.itertuples () Pandas, we can use DataFrame.itertuples () and then add each row’s data to the end of the list.

# import pandas as pd

import pandas as pd

 
# Create data frame

df = pd.DataFrame ({ ’Date ’ : [ ’ 10/2 / 2011’ , ’11/2 / 2011’ , ’ 12/2 / 2011’ , ’13/2 / 11’ ],

’Event’ : [ ’ Music’ , ’Poetry’ , ’ Theater’ , ’Comedy’ ],

  ’ Cost’ : [ 10000 , 5000 , 15000 , 2000 ]})

 
# Print the data frame

print (df)

Output:

We will now use DataFrame.itertuples () to iterate over each row of the given Dataframe and create a list from each row’s data.

# Create empty list

Row_list = []

 
# Iterate over each row

for rows in df.itertuples ():

# Create a list for the current line

my_list = [rows.Date, rows.Event, rows.Cost]

 

# add the list to final list

Row_list.append (my_list)

 
# Print list

print (Row_list)

Output:

As we can see in the output, we have successfully extracted each row of the given dataframe into a list. Like any other Python list, we can perform any operation on the list of the retrieved list.

# Find the length again
# list created

print ( len (Row_list))

  
# Print first 3 items

print (Row_list [: 3 ])

Output:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically