pandas converts rows into lists for reading and Nan processing

  • 2021-01-14 06:15:21
  • OfStack

pandas sometimes needs to read the contents of.csv files line by line, so how do you do that?

Let's do this all the way through, assuming we already have a.csv file.


# 1. Import packages 

import pandas as pd

# 2 Read the data 

readFile = pd.read_csv(' The output path ',encoding='gb2312')

for record in readFile.values:

   print(record)

This completes the process

What if there is Nan?

We can add the following after readFile:


readFile = readFile.fillna('del_token')
 in for record in readFile.values Back to join 1 a while cycle 
    record = list(record)

while ('del_token' in record):

   record.remove('del_token')
  print(record)
 The entire code should look something like this: 
# 1. Import packages 

import pandas as pd

# 2 Read the data 

readFile = pd.read_csv(' The output path ',encoding='gb2312')

#  to Nan The assignment 

readFile = readFile.fillna('del_token')
for record in readFile.values:

#  Convert list delete Nan

while ('del_token' in record):

   record.remove('del_token')
#  print 
   print(record)


Related articles: