Built with Tutify.io

    AIStudyLabs

    Pandas Convert Series to Json

    Pandas Convert Series to Json

    To convert a Pandas Series to JSON, you can use the series.to_json() method, which efficiently converts the Series into a JSON string. This method includes the key parameter orient, which accepts values such as 'index', ‘records’, ‘split’, ‘table’, and ‘values’. This functionality is especially useful for data exchange between different platforms or for saving data in a human-readable format.

    In this article, I will explain how to convert a Pandas Series into a JSON string. The Pandas Series.to_json() method returns a JSON-formatted string that represents the data in the Series, with the structure determined by the chosen orient and other parameters.

    Key Points –

    <a id="h-syntax-of-pandas-series-to-json"></a>

    Syntax of Pandas Series.to_json()

    Following is the syntax of the create Series.json() function.

    
    # Syntax of pandas Series.to_json()
    Series.to_json(path_or_buffer=None, orient='index', date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None)
    

    <a id="h-parameter-of-series-to-json"></a>

    Parameter of series.to_json()

    Following are the parameters of the series.to_json()

    <a id="h-return-value"></a>

    Return Value

    It returns a JSON string or a file-like object containing the Series data in the specified JSON format.

    <a id="h-usage-of-pandas-convert-series-to-json"></a>

    Usage of Pandas Convert Series to Json

    Series.to_json() is a method in Pandas that converts a Pandas Series to a JSON-formatted string. It allows for serializing a Series object into different JSON structures, such as key-value pairs, arrays, or tables, depending on the orient parameter.

    First, let’s create a Pandas Series from a list.

    
    import pandas as pd
      
    # Create the Series with a custom index
    series = pd.Series([20000, 25000, 30000, 28000, 23000], 
                    index=['Java', 'Spark', 'PySpark', 'Pandas', 'Python'])
    print("Original Series:\n",series)
    

    Yields below output.

    To convert a Pandas Series to JSON with orient='index', you can use the to_json() method with the orient parameter explicitly set to 'index'. This format treats the Series index as the keys and the Series values as the corresponding values in the JSON output.

    
    # Convert the Series to JSON with orient='index'
    ser = series.to_json(orient='index')
    print("After convert the Series to JSON:\n",ser)
    

    Here,

    <a id="h-convert-series-to-json-using-orient-split"></a>

    Convert Series to JSON Using orient='split'

    To convert a Pandas Series to JSON using orient='split', you can set the orient parameter to 'split' in the to_json() method. This format divides the Series into a JSON object with separate keys for the index and data.

    
    # Convert the Series to JSON with orient='split'
    ser = series.to_json(orient='split')
    print("After converting series to JSON string:\n",ser)
    
    # Output:
    # After converting series to JSON string:
    # {"name":null,"index":["Java","Spark","PySpark","Pandas","Python"],"data":[20000,25000,30000,28000,23000]}
    

    Here,

    <a id="h-using-orient-records"></a>

    Using orient='records'

    To convert a Pandas Series to JSON using orient='records', set the orient parameter to 'records' in the to_json() method. This format converts the Series into a list of dictionaries, where each dictionary represents an item in the Series.

    
    # Convert the Series to JSON with orient='records'
    ser = series.to_json(orient='records')
    print("After converting series to JSON string:\n",ser)
    
    # Output:
    # After converting series to JSON string:
    # [{"Java":20000},{"Spark":25000},{"PySpark":30000},{"Pandas":28000},{"Python":23000}]
    

    Here,

    <a id="h-using-orient-values"></a>

    Using orient='values'

    To convert a Pandas Series to JSON using orient='values', set the orient parameter to 'values' in the to_json() method. This format outputs the Series values as a simple JSON array, without including the index labels.

    
    # Convert the Series to JSON with orient='values'
    ser = series.to_json(orient='values')
    print("After converting series to JSON string:\n",ser)
    
    # Output:
    # After converting series to JSON string:
    # [20000,25000,30000,28000,23000]
    

    Here,

    <a id="h-using-orient-table"></a>

    Using orient = ‘table’

    To convert a Pandas Series to JSON using orient='table', set the orient parameter to 'table' in the to_json() method. This format is structured to represent the Series in a tabular format, which is compatible with the JSON Table Schema. It includes both the index and the values in a structured way.

    
    # Convert the Series to JSON with orient='table'
    ser = series.to_json(orient='table')
    print("After converting series to JSON string:\n",ser)
    
    # Output:
    # After converting series to JSON string:
     {"schema":{"fields":[{"name":"index","type":"string"},{"name":"values","type":"integer"}],"primaryKey":["index"],"pandas_version":"0.20.0"},"data":[{"index":"Java","values":20000},{"index":"Spark","values":25000},{"index":"PySpark","values":30000},{"index":"Pandas","values":28000},{"index":"Python","values":23000}]}
    

    Here,

    <a id="h-faq-on-pandas-convert-series-to-json"></a>

    FAQ on Pandas Convert Series to Json

    To convert a Pandas Series to a JSON string, you can use the .to_json() method.

    By default, the .to_json() method converts the series into a dictionary where the index becomes the keys and the values are the data in the series.

    By default, the index is included in the JSON output when you convert a Pandas Series to JSON using the .to_json() method. The index is used as the key, and the series values are the corresponding values in the JSON object.

    You can handle missing values by specifying the date_format or default_handler parameter in .to_json(). If you have NaN or None values in your series, they will be converted to null in the JSON output.

    To pretty-print the JSON output after converting a Pandas Series to JSON, you can use Python's built-in json module.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned how to convert a Pandas Series to JSON using the series.to_json() method, along with various examples. For more params use to_json() method from the pandas reference.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References