将 DataFrame 的 DataFrame 转换为单个 DataFrame 仅选择某些列 Python
Transform DataFrame of Dataframe into Single DataFrame Selecting only Some Columns Python
我有下一个Json文件:
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
但是,我只想 select 这些过滤器中的 2 个,按 filterType 名称,我想要“PRICE_FILTER”和“LOT_SIZE”
您需要列出所需的过滤器,然后select列表中的任何过滤器:
filters = ['PRICE_FILTER', 'LOT_SIZE']
df = df[df['filters'].str.isin('|'.join(filters))]
要仅从“PRICE_FILTER”和“LOT_SIZE”过滤器中获取列,请尝试:
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)
打印:
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
我有下一个Json文件:
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
但是,我只想 select 这些过滤器中的 2 个,按 filterType 名称,我想要“PRICE_FILTER”和“LOT_SIZE”
您需要列出所需的过滤器,然后select列表中的任何过滤器:
filters = ['PRICE_FILTER', 'LOT_SIZE']
df = df[df['filters'].str.isin('|'.join(filters))]
要仅从“PRICE_FILTER”和“LOT_SIZE”过滤器中获取列,请尝试:
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)
打印:
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