即使将replace inplace设置为true,Pandas Dataframe fillna也无法正常工作

时间:2018-07-23 20:24:46

标签: python-3.x pandas csv dataframe fillna

程序从RESTApi检索JSON数据

import requests
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows',1000)
url = 'http://xxxxxxxxxxx:7180/api/v15/clusters/cluster/services/impala/impalaQueries?from=2018-07-23T14:00:00&to=2018-07-23T21:00:00&filter=(query_state+%3D+%22FINISHED%22)&offset=0&limit=1000'
username = 'xxxxx'
password = 'xxxxx'
result = requests.get(url, auth=(username,password))
outJSON = result.json()
df = pd.io.json.json_normalize(outJSON['queries'])
filename ="tempNew.csv"
df.to_csv(filename)

CSV数据在某些字段中包含空值,在少数字段中包含NaN。

  

输入

Admitted immediately,,BLAHBLAH,0,NaN,0,0,0,0,0.0,,,,

使用fillna将所有Nulls和NaN替换为0时,因为它们是目标表中的数字字段。

尝试代码:

for col in df:
   df[col].fillna(0,inplace=True)

df.fillna(0,inplace=True)
  

输出

     
    

“立即接受”,“ 0”,“ BLAHBLAH”,“ 0”,“ NaN”,“ 0”,“ 0”,“ 0”,     '0','0.0','0','0','0'

  

如何确保数据框中的所有NaN值都更改为0,因为它们要加载的表由于NaN值而拒绝了值?

我从RESTAPI逐行处理数据切换到Dataframe,印象是使用DF更容易处理数据。如果fillna无法正常工作,是否有更好的方法来处理df中的数据而不逐行迭代?

  

更新

df = pd.io.json.json_normalize(outJSON['queries'])
fname = "WithouFilna_1.csv"
df.to_csv(fname)
df.fillna(0,inplace=True)
filename ="fillna_1.csv"
df.to_csv(filename)
  

我尝试在df.fillna之前和之后编写输出。在几个字段中看到部分更改,但在所有字段中却看不到

     

之前:

859,Unknown,,,2,0,xxxx,RESET_METADATA,,,,,,,,,,,,,,
860,Admitted immediately,0,,1,2,xxxx,,0,,NaN,0,0,,0
861,Admitted immediately,0,,0,0,xxxx,,0,,NaN,0,0,,0
  

之后:

859,Unknown,0,0,2,0,xxxx,RESET_METADATA,0,,0,0,0,0,0,0,0,0,0,0,0
860,Admitted immediately,0,0,1,2,xxx,0,0,,NaN,0,0,0,0,0,0,0,0,0
861,Admitted immediately,0,0,0,0,xxx,0,0,,NaN,0,0,0,0,0,0,0,0,0
  

df.dtypes输出

attributes.admission_result                              object
attributes.admission_wait                                object
attributes.bytes_streamed                                object
attributes.client_fetch_wait_time                        object
attributes.client_fetch_wait_time_percentage             object
attributes.connected_user                                object
attributes.ddl_type                                      object
attributes.estimated_per_node_peak_memory                object
attributes.file_formats                                  object
attributes.hdfs_average_scan_range                       object
attributes.hdfs_bytes_read                               object
attributes.hdfs_bytes_read_from_cache                    object
attributes.hdfs_bytes_read_from_cache_percentage         object
attributes.hdfs_bytes_read_local                         object
attributes.hdfs_bytes_read_local_percentage              object
attributes.hdfs_bytes_read_remote                        object
attributes.hdfs_bytes_read_remote_percentage             object
attributes.hdfs_bytes_read_short_circuit                 object
attributes.hdfs_bytes_read_short_circuit_percentage      object
attributes.hdfs_scanner_average_bytes_read_per_second    object
  

df.values [5:6,:15]

array([['Unknown', nan, nan, '1', '8', 'xxxxx',
        'SHOW_DBS', nan, '', nan, nan, nan, nan, nan, nan]], dtype=object)

1 个答案:

答案 0 :(得分:0)

问题归结于restAPI返回不一致的数据。来自API的受影响字段的数据为“ NaN”

df.fillna(0, inplace=True)期间显然没有考虑过

我使用以下方法解决了这个问题:

df.replace({'NaN': '0'}, regex=True, inplace=True)