In [1]:
# Writing a DataFrame to a JSON File
import pandas as pd
data = {
''''Name'''': [''''Alice'''', ''''Bob'''', ''''Charlie''''],
''''Age'''': [25, 30, 35],
''''City'''': [''''New York'''', ''''Los Angeles'''', ''''Chicago'''']
}
df = pd.DataFrame(data)
df.to_json(''''sample1.json'''', orient=''''records'''')
print(df)
Name Age City 0 Alice 25 New York 1 Bob 30 Los Angeles 2 Charlie 35 Chicago
In [2]:
# Writing a DataFrame to a JSON File with Different Orientations
import pandas as pd
data = {
''''Name'''': [''''Alice'''', ''''Bob'''', ''''Charlie''''],
''''Age'''': [25, 30, 35],
''''City'''': [''''New York'''', ''''Los Angeles'''', ''''Chicago'''']
}
df = pd.DataFrame(data)
df.to_json(''''sample2_records.json'''', orient=''''records'''')
df.to_json(''''sample2_split.json'''', orient=''''split'''')
df.to_json(''''sample2_index.json'''', orient=''''index'''')
df.to_json(''''sample2_columns.json'''', orient=''''columns'''')
df.to_json(''''sample2_values.json'''', orient=''''values'''')
print(df)
Name Age City 0 Alice 25 New York 1 Bob 30 Los Angeles 2 Charlie 35 Chicago
In [3]:
# Writing a DataFrame to a JSON File with Indentation
import pandas as pd
data = {
''''Name'''': [''''Alice'''', ''''Bob'''', ''''Charlie''''],
''''Age'''': [25, 30, 35],
''''City'''': [''''New York'''', ''''Los Angeles'''', ''''Chicago'''']
}
df = pd.DataFrame(data)
df.to_json(''''sample3.json'''', orient=''''records'''', indent=4)
print(df)
Name Age City 0 Alice 25 New York 1 Bob 30 Los Angeles 2 Charlie 35 Chicago
In [4]:
# Writing a DataFrame to a JSON File with Compression
import pandas as pd
data = {
''''Name'''': [''''Alice'''', ''''Bob'''', ''''Charlie''''],
''''Age'''': [25, 30, 35],
''''City'''': [''''New York'''', ''''Los Angeles'''', ''''Chicago'''']
}
df = pd.DataFrame(data)
df.to_json(''''sample4.json.gz'''', orient=''''records'''', lines=True, compression=''''gzip'''')
print(df)
Name Age City 0 Alice 25 New York 1 Bob 30 Los Angeles 2 Charlie 35 Chicago
In [5]:
# Reading JSON from a File
import pandas as pd
df = pd.read_json(''''sample1.json'''')
print(df)
Name Age City 0 Alice 25 New York 1 Bob 30 Los Angeles 2 Charlie 35 Chicago
In [6]:
# Reading JSON from a String
import pandas as pd
import json
import io
json_str = io.StringIO(u''''[{"name": "John", "age": 30, "city": "New York"},{"name": "Anna", "age": 22, "city": "London"},{"name": "Mike", "age": 32, "city": "San Francisco"}]'''')
df = pd.read_json(json_str)
print(df)
name age city 0 John 30 New York 1 Anna 22 London 2 Mike 32 San Francisco
In [7]:
# Reading Nested JSON
import pandas as pd
nested_json = io.StringIO(u''''{"students": [{"name": "John", "age": 30, "city": "New York"},{"name": "Anna", "age": 22, "city": "London"},{"name": "Mike", "age": 32, "city": "San Francisco"}]}'''')
df = pd.json_normalize(pd.read_json(nested_json)[''''students''''])
print(df)
name age city 0 John 30 New York 1 Anna 22 London 2 Mike 32 San Francisco
In [8]:
# Reading JSON from a URL
import pandas as pd
#url = ''''https://api.example.com/data.json''''
url="sample3.json"
df = pd.read_json(url)
print(df)
Name Age City 0 Alice 25 New York 1 Bob 30 Los Angeles 2 Charlie 35 Chicago