I have a pandas dataframe that is used to create a JSON which in turn is used to display a highcharts chart.
Pandas dataframe:
Date colA colB
12-Sep-14 20 40
13-Sep-14 50 10
14-Sep-14 12 -20
15-Sep-14 74 43
Is there a way to change some of the colA and colB values to null. The reason for this is that I ultimately need a JSON that looks something like this:
[
[12-Sep-14, 20, 40],
[13-Sep-14, null, null],
[14-Sep-14, 12, -20],
[15-Sep-14, 74, 43]
]
The reason for this is that I require a highcharts chart where certain plot points are blank. To do this, you specify the date followed by null.
So I need to somehow update certain values in the pandas dataframe so that once I convert it to a JSON using .to_json() then the json will contain the specified null values as per the example above.
Thanks for any suggestions.
While making a Data Frame from a csv file, many blank columns are imported as null value into the Data Frame which later creates problems while operating that data frame. Pandas isnull() and notnull() methods are used to check and manage NULL values in a data frame.
Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it's another beast entirely. As the null in Python, None is not defined to be 0 or any other value.
There are multiple ways to add a new empty/blank column (single or multiple columns) to a pandas DataFrame by using assign operator, assign() , insert() and apply() methods. By using these you can add one or multiple empty columns with either NaN , None , Blank or Empty string values to all cells.
Count missing values in each row and column Since sum() calculate as True=1 and False=0 , you can count the number of missing values in each row and column by calling sum() from the result of isnull() . You can count missing values in each column by default, and in each row with axis=1 .
Try using NaN
which is the Pandas missing value:
from numpy import nan
df = pd.read_clipboard()
df.colA.iloc[1] = NaN
instead of NaN
you could also use None
. Note that neither of these terms are entered with quotes.
Then you can use to_json()
to get your output:
df.to_json()
'{"Date":{"0":"12-Sep-14","1":"13-Sep-14","2":"14-Sep-14","3":"15-Sep-14"},"colA":{"0":20.0,"1":null,"2":12.0,"3":74.0},"colB":{"0":40,"1":10,"2":-20,"3":43}}'
Does this work?
import pandas as pd
# Read in data frame from clipboard
df = pd.read_clipboard()
df = df.replace(df.iloc[1][1:],'null')
Date colA colB
0 12-Sep-14 20 40
1 13-Sep-14 null null
2 14-Sep-14 12 -20
3 15-Sep-14 74 43
Here, df.iloc[1] gives access to row 1
Finally,
df.to_json(orient='values').replace("\"","")
gives json without the ""
[[12-Sep-14,20,40],[13-Sep-14,null,null],[14-Sep-14,12,-20],[15-Sep-14,74,43]]
Code as below:
import numpy as np
# create null/NaN value with np.nan
df.loc[1, colA:colB] = np.nan
Here's the explanation:
df.loc[1, colA:colB]
means selecting row 1 and columns from colA to colB;np.nan
to the specific location.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With