Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 即使“替换就地”设置为“真”,数据帧填充仍不一致_Python 3.x_Pandas_Csv_Dataframe_Fillna - Fatal编程技术网

Python 3.x 即使“替换就地”设置为“真”,数据帧填充仍不一致

Python 3.x 即使“替换就地”设置为“真”,数据帧填充仍不一致,python-3.x,pandas,csv,dataframe,fillna,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&am

程序从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数据包含某些字段的Null值,以及少数字段的NaN值

输入

Admitted immediately,,BLAHBLAH,0,NaN,0,0,0,0,0.0,,,,
使用fillna将所有NULL&NaN替换为0时,因为它们是目标表中的数字字段

尝试过的代码:

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

df.fillna(0,inplace=True)
输出

Admitted immediately,,BLAHBLAH,0,NaN,0,0,0,0,0.0,,,,
“立即入院”,“0”,“BLAHBLAH”,“0”,“NaN”,“0”,“0”,“0”, “0”、“0.0”、“0”、“0”、“0”、“0”

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

我从RESTAPI逐行处理数据切换到Dataframe,感觉使用DF处理数据更容易。如果fillna不起作用,有没有更好的方法在不逐行迭代的情况下对df中的数据进行按摩

更新:

我尝试在之前和之后编写df.fillna的输出。少数字段出现了部分更改,但并非所有字段都出现了部分更改

之前:

之后:

df.dtypes输出

df.值[5:6,:15]


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

df.fillna(0,inplace=True)

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

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

您显示的“输出”使它看起来像每个元素实际上都是一个字符串,这可以解释为什么fillna不做任何事情——实际上没有空值可填充——但由于这不是标准的输出格式,所以很难判断这是实际发生的还是您为显示目的引入的。请编辑您的问题,以包含从调用(如
print(df.values)
)中获得的输出。如果您能弄清楚哪个代码准确地生成此输出,这将非常有帮助。@christinabo添加了提取数据的代码。
fillna
仅填充
nan
值。因此,您可以将空单元格更改为nan并填充它们,即
df[df==“”| df=='nan]=np.nan;df.fillna(0)
或直接用零填充空单元格。ie
df[df==”“| df=='NaN]=0
。还要注意,
fillna
是矢量化的。不需要for循环。fillna将处理整个数据帧,而不仅仅是一个数据帧column@Dvusrgme:您可以切片显示帧中重要的部分,如
df.values[5:6,:5]
等。基本上,我们只需要确认您的NaN实际上只是一个字符串。
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
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
array([['Unknown', nan, nan, '1', '8', 'xxxxx',
        'SHOW_DBS', nan, '', nan, nan, nan, nan, nan, nan]], dtype=object)
df.replace({'NaN': '0'}, regex=True, inplace=True)