0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DataFrameで'nan'が消えてしまうのを直す。

Last updated at Posted at 2020-07-07

CSVの段階で"nan"という文字列を利用していたのに、DataFrameに入れたときに勝手に'NaN'になってしまうので困った。
もう一度"nan"を利用するためには、fillna('nan')を使うことで解決。
まぁ空の部分も'nan'になるんですけど

import math
import numpy as np
import pandas as pd
# データの作成
data = [
    ['', 1, '2', 3],  #
    [math.nan, np.nan, pd.np.nan, 'np.nan'],
    [None, None, 'nan', 'nan']
]
df = pd.DataFrame(data)
df.to_csv('test.csv')

# CSVにある'nan'を読み込むとNaNになる
df = pd.read_csv('test.csv')
print(df)
#   0    1    2       3
# 0 NaN  1.0  2.0       3
# 1 NaN  NaN  NaN  np.nan
# 2 NaN  NaN  NaN     NaN

# fillnaをするとnanになる
na = df.fillna('nan')
print(na)
#     0    1    2       3
#0  nan    1    2       3
#1  nan  nan  nan  np.nan
#2  nan  nan  nan     nan
0
0
7

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?