Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I'm using the pandas library to read in some CSV data. In my data, certain columns contain strings. The string "nan" is a possible value, as is an empty string. I managed to get pandas to read "nan" as a string, but I can't figure out how to get it not to read an empty value as NaN. Here's sample data and output

One,Two,Three

a,1,one

b,2,two

,3,three

d,4,nan

e,5,five

nan,6,

g,7,seven

>>> pandas.read_csv('test.csv', na_values={'One': [], "Three": []})

    One  Two  Three

0    a    1    one

1    b    2    two

2  NaN    3  three

3    d    4    nan

4    e    5   five

5  nan    6    NaN

6    g    7  seven

It correctly reads "nan" as the string "nan', but still reads the empty cells as NaN. I tried passing in str in the converters argument to read_csv (with converters={'One': str})), but it still reads the empty cells as NaN.

I realize I can fill the values after reading, with fillna, but is there really no way to tell pandas that an empty cell in a particular CSV column should be read as an empty string instead of NaN?

1 Answer

0 votes
by (41.4k points)

Using read_csv:

df = pd.read_csv('test.csv', na_filter= False)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 10, 2019 in Data Science by ashely (50.2k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...