Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用pandas对数据进行过滤和排序_Python_Json_Pandas - Fatal编程技术网

Python 使用pandas对数据进行过滤和排序

Python 使用pandas对数据进行过滤和排序,python,json,pandas,Python,Json,Pandas,我想从中获取部分信息,但我不知道如何过滤数据(我只想获取选定的值,如果键不包含“BTC”字符串,则不获取值) 我正在尝试这样做: {"BTC_MINT":{"volume":11.00, "high24":0.002, "low24":0.001}, "BTC_NOTE":{"volume":11.00, "high24":0.002, "low24":0.001}} 我从熊猫开始,但我不知道这是否是正确的方式 link = 'https://poloniex.com/public?comm

我想从中获取部分信息,但我不知道如何过滤数据(我只想获取选定的值,如果键不包含“BTC”字符串,则不获取值) 我正在尝试这样做:

{"BTC_MINT":{"volume":11.00, "high24":0.002, "low24":0.001},
 "BTC_NOTE":{"volume":11.00, "high24":0.002, "low24":0.001}}
我从熊猫开始,但我不知道这是否是正确的方式

link = 'https://poloniex.com/public?command=returnTicker'
with urllib.request.urlopen(link) as rawdata:
    data = rawdata.readall().decode()
data = json.loads(data)
print(data.items())
data = pd.DataFrame([[cur, last, volume, high24, low24] 
                     for cur, d in data.items() 
                     for last, x, x, x, volume, x, x, high24, low24 in d.items()])
不幸的是,这段代码不起作用。我发现以下错误:

[cur, last, volume, high24, low24] for cur, d, x, w, d, q in data.items() for last, x, x, x, volume, x, x, high24, low24 in d.items()
ValueError: need more than 2 values to unpack

谁能帮我一下,告诉我该怎么做

您只需将字典(数据)传递给pd.Dataframe即可创建数据帧。如果要将其子集为仅包含包含字符串BTC的列,可以执行以下操作:

df = pd.DataFrame(data)
new_cols = [x for x in df.columns if x.find('BTC') > -1]
new_df = df[new_cols]
要仅获取索引中以
BTC
开头的名称,请执行以下操作:

>>> df[df.index.str.startswith('BTC')].head()

          baseVolume    high24hr     low24hr
BTC_1CR   0.00000000  0.00000000  0.00000000
BTC_ABY   0.01968682  0.00000020  0.00000019
BTC_ADN   0.00000000  0.00000000  0.00000000
BTC_ARCH  0.07205024  0.00004813  0.00004693
BTC_BBR   0.19846259  0.00002123  0.00002115
>>> df[df.index.str.startswith('BTC')].head()

          baseVolume    high24hr     low24hr
BTC_1CR   0.00000000  0.00000000  0.00000000
BTC_ABY   0.01968682  0.00000020  0.00000019
BTC_ADN   0.00000000  0.00000000  0.00000000
BTC_ARCH  0.07205024  0.00004813  0.00004693
BTC_BBR   0.19846259  0.00002123  0.00002115