Change language

Create a List of Strings in a Pandas DataFrame | Set 2

| |

Note. To link to the CSV file used in the code, click here .

Solution # 1: To access the data of each row of DataFrame.iloc Pandas data, we can use the DataFrame attribute .iloc 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 ]})

 
# Raspe chat dataframe

print (df)

Output:

We will now use the DataFrame.iloc attribute to access the values ​​of each row in the data frame, and then DataFrame.iloc a list from it.

# Create empty list

Row_list = []

  
# Iterate over each line

for i in range ((df.shape [ 0 ])):

  

# Using iloc to access values ​​

# current line denoted by & quot; i & quot;

Row_list.append ( list (df.iloc [i,:]))

 
# 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:

Solution # 2. To access your data every DataFrame.iat row of Pandas data, we can use the DataFrame.iat attribute 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 ]})

 
# Create empty list

Row_list = []

  
# Iterate over each line

for i in range ((df.shape [ 0 ])):

# Create a list to store data

Current line no.

  cur_row = []

 

# iterate over all columns

for j in range (df.shape [ 1 ]):

 

# add each data

# column to list

cur_row.append (df.iat [i, j])

  

  # add current line to list

Row_list.append (cur_row)

 
# Print list

print (Row_list)

Output:

# Find the length again
# list created

print ( len (Row_list))

 
# Print first 3 items

print (Row_list [: 3 ])

Output:

Create a List of Strings in a Pandas DataFrame | Set 2 File handling: Questions

Create a List of Strings in a Pandas DataFrame | Set 2 Python functions: Questions

Shop

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

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method