[英]How can I manipulate the following JSON into the desired Pandas Dataframe?
Here is the JSON I am working with:这是我正在使用的 JSON:
{ "2021-05-24": { "RSI": "75.1354" }, "2021-05-21": { "RSI": "74.5561" }, "2021-05-14": { "RSI": "78.1510" }, "2021-05-07": { "RSI": "75.3214" }, "2021-04-30": { "RSI": "74.1698" } }
I need to transform this JSON into the Following Pandas Dataframe:我需要将此 JSON 转换为以下 Pandas Dataframe:
Date RSI
0 2021-05-24 75.1354
1 2021-05-21 74.5561
2 2021-05-14 78.1510
...
The dataframe that pandas is defaulting to: dataframe pandas 默认为:
2021-05-24 2021-05-21 2021-05-14
RSI 37.8626 39.4409 40.3654
Basically I want to filter the dataframe to exclude RSI Values that are >25 and < 75. If I have the desired dataframe, I can simply filter by the 'RSI' Index.基本上我想过滤 dataframe 以排除 >25 和 < 75 的 RSI 值。如果我有所需的 dataframe,我可以简单地按“RSI”索引过滤。 However, I cannot figure out how to filter the current dataframe.
但是,我无法弄清楚如何过滤当前的 dataframe。 Any help is appreciated, and I am pretty new to Pandas.
感谢任何帮助,我对 Pandas 很陌生。
Try with尝试
out = df.T.reset_index()
data={
"2021-05-24": {
"RSI": "75.1354"
},
"2021-05-21": {
"RSI": "74.5561"
},
"2021-05-14": {
"RSI": "78.1510"
},
"2021-05-07": {
"RSI": "75.3214"
},
"2021-04-30": {
"RSI": "74.1698"
}
}
You can use DataFrame.from_dict()
method:您可以使用
DataFrame.from_dict()
方法:
df=pd.DataFrame.from_dict(data,orient='index')
OR或者
Use Transpose attribute:使用转置属性:
df=pd.Dataframe(data).T
OR或者
Use transpose()
method:使用
transpose()
方法:
df=pd.DataFrame(data).transpose()
You can use pandas.DataFrame.melt()
您可以使用
pandas.DataFrame.melt()
df = pd.read_json('<your_file>.json')
df.melt(var_name='Date', value_name='RSI')
Date RSI
0 2021-05-24 75.1354
1 2021-05-21 74.5561
2 2021-05-14 78.1510
3 2021-05-07 75.3214
4 2021-04-30 74.1698
Or, because your json orientation is index
, you can set the orient
parameter of pd.read_json()
to "index"
或者,因为您的 json 方向是
index
,您可以将pd.read_json()
的orient
参数设置为"index"
pd.read_json('<your_file>.json', orient='index')
RSI
2021-04-30 74.1698
2021-05-07 75.3214
2021-05-14 78.1510
2021-05-21 74.5561
2021-05-24 75.1354
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.