DataFrame 的 DataFrame Python

DataFrame of DataFrames Python

我有这个 Json 文件,

{
  "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"}]
    }

我已将 'symbols' 从 Json 文件转换为 DataFrame:

with open('exchangeInfo.json', 'r') as fp:
    exchangeInfo = json.load(fp)


symbolsDF = pd.DataFrame(client.get_exchange_info(['symbols'])

我想将 'filters' 列从 'symbols' 转换为这样的列

"symbols": [
    {
      "symbol": "ETHBTC",
      "status": "TRADING",
      "baseAsset": "ETH",
      "baseAssetPrecision": 8,
      "quoteAsset": "BTC",
      "quotePrecision": 8,
      "quoteAssetPrecision": 8,
      "baseCommissionPrecision": 8,
      "quoteCommissionPrecision": 8,
      "minPrice": "0.00000100",
      "maxPrice": "100000.00000000",
      "tickSize": "0.00000100",
      "minQty": "0.00100000",
      "maxQty": "100000.00000000",
      "stepSize": "0.00100000"}]
    }

所以我最终的 DataFrame 将由符号组成,列将是

["symbol", "status", "baseAsset", "baseAssetPrecision", "quoteAsset","quotePrecision", "quoteAssetPrecision", "baseCommissionPrecision", "quoteCommissionPrecision", "minPrice", "maxPrice", "tickSize", "minQty", "maxQty", "stepSize"]

谢谢

尝试:

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",
                },
            ],
        },
    ],
}

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)

打印:

   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