带有NaN的csv文件为空,里面没有任何内容

时间:2018-08-24 07:25:08

标签: python pandas csv

有没有办法像这样将所有NaN更改为空

我尝试过:

data = pd.read_csv('iso_country_continent.csv', keep_default_na=False,  na_values=[' '])

当前输出:

 Unnamed: 0 geo_country        country continent
160         160          MX         Mexico       N_A
161         161          MY       Malaysia        AS
162         162          MZ     Mozambique        AF
163         163         Nan         Namibia        AF
164         164          NC  New Caledonia        OC
165         165          NE          Niger        AF

预期输出:

 Unnamed: 0 geo_country        country continent
160         160          MX         Mexico       N_A
161         161          MY       Malaysia        AS
162         162          MZ     Mozambique        AF
163         163                    Namibia        AF
164         164          NC  New Caledonia        OC
165         165          NE          Niger        AF

但是我收到类似

的错误
  

AttributeError:'NoneType'对象没有属性'find'

1 个答案:

答案 0 :(得分:1)

如果将Sub Macro2() Dim arr_B As Variant Dim arr_E As Variant Dim i As Long Dim k As Long Dim element As Variant arr_B = Array("B1_RSD1", "B2_RSD2") arr_E = Array("E1_RSD1", "E2_RSD2") For Each element In arr_B For Each element In arr_E Set rngStart = ActiveDocument.Bookmarks(i).Range Set rngEnd = ActiveDocument.Bookmarks("k").Range ActiveDocument.Range(rngStart.Start, rngEnd.End).Select Selection.Delete Exit For Next k Next i End Sub 的值写入熊猫文件,它们将转换为空字符串。

所以看来您的值不是缺少值,而是字符串。

因此需要参数NaN才能将其转换为keep_default_na=False, na_values=['Naan'],然后:

NaN

replace

data = data.fillna('')

但是如果data = pd.read_csv('iso_country_continent.csv') data = data.replace({'Naan':''}) 中的空值仅需要参数csv,但没有任何值会转换为keep_default_na=False

或指定要通过NaN

进行转换的值