I have the next Json File:
exchangeInfo = {
"timezone": "UTC",
"serverTime": 1565246363776,
"rateLimits": [],
"exchangeFilters": [],
"symbols": [
{
"symbol": "ETHBTC",
"status": "TRADING",
"baseAsset": "ETH",
"baseAssetPrecision": 8,
"quoteAsset": "BTC",
"quotePrecision": 8,
"quoteAssetPrecision": 8,
"baseCommissionPrecision": 8,
"quoteCommissionPrecision": 8,
"filters": [
{
"filterType": "PRICE_FILTER",
"minPrice": "0.00000100",
"maxPrice": "100000.00000000",
"tickSize": "0.00000100",
},
{
"filterType": "PERCENT_PRICE",
"multiplierUp": "1.3000",
"multiplierDown": "0.7000",
"avgPriceMins": 5,
},
{
"filterType": "LOT_SIZE",
"minQty": "0.00100000",
"maxQty": "100000.00000000",
"stepSize": "0.00100000",
},
],
},
],
}
And applying the next code, I transform the column "filters" into columns.
df = pd.json_normalize(exchangeInfo["symbols"])
df = pd.concat(
[
df,
df.pop("filters")
.apply(lambda x: dict(i for d in x for i in d.items()))
.apply(pd.Series),
],
axis=1,
).drop(columns="filterType")
print(df)
Prints:
symbol status baseAsset baseAssetPrecision quoteAsset quotePrecision quoteAssetPrecision baseCommissionPrecision quoteCommissionPrecision minPrice maxPrice tickSize multiplierUp multiplierDown avgPriceMins minQty maxQty stepSize
0 ETHBTC TRADING ETH 8 BTC 8 8 8 8 0.00000100 100000.00000000 0.00000100 1.3000 0.7000 5 0.00100000 100000.00000000 0.00100000
But, I would like to select only 2 of those filters, by filterType name, I would like to have "PRICE_FILTER" and "LOT_SIZE"
To get columns from only "PRICE_FILTER" and "LOT_SIZE" filters, try:
df = pd.json_normalize(exchangeInfo["symbols"])
df = pd.concat(
[
df,
df.pop("filters")
.apply(
lambda x: dict(
i
for d in x
for i in d.items()
if d["filterType"] in {"PRICE_FILTER", "LOT_SIZE"}
)
)
.apply(pd.Series),
],
axis=1,
).drop(columns="filterType")
print(df)
Prints:
symbol status baseAsset baseAssetPrecision quoteAsset quotePrecision quoteAssetPrecision baseCommissionPrecision quoteCommissionPrecision minPrice maxPrice tickSize minQty maxQty stepSize
0 ETHBTC TRADING ETH 8 BTC 8 8 8 8 0.00000100 100000.00000000 0.00000100 0.00100000 100000.00000000 0.00100000