The program retrieves JSON data from RESTApi
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 data contains nulls for some fields and NaN for few fields.
Input:
Admitted immediately,,BLAHBLAH,0,NaN,0,0,0,0,0.0,,,,
While using fillna to replace all Nulls & NaN to 0 because they are number fields in the target Table.
Tried Codes:
for col in df:
df[col].fillna(0,inplace=True)
df.fillna(0,inplace=True)
Output:
'Admitted immediately', '0', 'BLAHBLAH', '0', 'NaN', '0', '0', '0', '0', '0.0', '0','0','0'
How can I ensure that all NaN values are changed to 0 in my dataframe because the table they are loading to is rejecting values because of NaN values?
I switched from processing data line by line from RESTAPI to Dataframe with an impression that its easier to massage data using DF. Is there a better way to massage data in a df without iterating row by row if the fillna won't work?
Update:
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)
I tried to write the output of df.fillna before and after. Partial changes are seen for few fields, but not for all of them
Before:
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
After:
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 Output
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)