简体   繁体   中英

Python & Pandas: How to address NaN values in a loop?

With Python and Pandas I'm seeking to take values from CSV cells and write them as txt files via a loop. The structure of the CSV file is:

user_id,    text,   text_number
0,  test text A,    text_0
1,      
2,      
3,      
4,      
5,  test text B,    text_1

The script below successfully writes a txt file for the first row - it is named text_0.txt and contains test text A .

import pandas as pd

df= pd.read_csv("test.csv", sep=",")

for index in range(len(df)):
     with open(df["text_number"][index] +  '.txt', 'w') as output:
        output.write(df["text"][index])

However, I receive an error when it proceeds to the next row:

TypeError: write() argument must be str, not float

I'm guessing the error is generated when it encounters values it reads as NaN . I attempted to add the dropna feature per the pandas documentation like so:

import pandas as pd

df= pd.read_csv("test.csv", sep=",")

df2 = df.dropna(axis=0, how='any')

for index in range(len(df)):
     with open(df2["text_number"][index] +  '.txt', 'w') as output:
        output.write(df2["text"][index])

However, the same issue persists - a txt file is created for the first row, but a new error message is returned for the next row: KeyError: 1 .

Any suggestions? All assistance greatly appreciated.

The issue here is that you are creating a range index which is not necessarily in the data frame's index. For your use case, you can just iterate through rows of data frame and write to the file.

for t in df.itertuples():
    if t.text_number:           # do not write if text number is None
        with open(t.text_number +  '.txt', 'w') as output:
            output.write(str(t.text))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM