Harshil Shah
Harshil Shah

Reputation: 537

Special values (NaN) are not being copied while saving as a CSV file

I have a few text files which I am reading using pd.read_csv and then I am performing the following code to merge them

    import pandas as pd

    data = pd.read_csv('\\cbh.txt', sep="\t")
    cbh = data['cbh'].to_frame()
    time = data['time'].to_frame()
    latitude = data['latitude'].to_frame()
    longitude = data['longitude'].to_frame()

    data1 = pd.read_csv('\\e.txt', sep="\t")
    e = data1['e'].to_frame()

    main1 = time.join(latitude)
    main2 = main1.join(longitude)
    main3 = main2.join(cbh)
    main4 = main3.join(e)
    print(main4)

    main4.to_csv(r'\\main.csv', header=True, index=False, mode='w', sep=',')

But while saving the file as a CSV the special values (NaN) are not stored

Following is the output when I print the merged data

      time   latitude  longitude           cbh         e
0    61359  23.459999  72.300003   7985.723352 -0.000256
1    61359  23.459999  72.425003   9758.239313 -0.000275
2    61359  23.459999  72.550003  11570.430130 -0.000307
3    61359  23.334999  72.300003   7914.476258 -0.000281
4    61359  23.334999  72.425003   9728.343473 -0.000313
5    61359  23.334999  72.550003  11562.327510 -0.000352
6    61359  23.209999  72.300003   7882.624615 -0.000320
7    61359  23.209999  72.425003   9718.564460 -0.000359
8    61359  23.209999  72.550003  11554.504300 -0.000398
9    61359  23.084999  72.300003   7871.169200 -0.000365
10   61359  23.084999  72.425003   9708.785447 -0.000404
11   61359  23.084999  72.550003  11546.401690 -0.000443
12   61359  22.959999  72.300003   7859.713785 -0.000397
13   61359  22.959999  72.425003   9699.006434 -0.000437
14   61359  22.959999  72.550003  11538.578480       NaN

Following is the file I get after saving it as a CSV

enter image description here

Upvotes: 0

Views: 58

Answers (1)

rrcal
rrcal

Reputation: 3752

NaN is the absence of a value (number) in this case, and you save to CSV format it will simple be kept empty, with two separators next to each other: ,,. If you read that file back into python using pandas you should have the same NaN values, however, if you open on Excel or other tool, it might just appear as blank.

Different tools display missing value in different ways.

Upvotes: 1

Related Questions