Here is the JSON I am working with:
{ "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:
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:
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. However, I cannot figure out how to filter the current dataframe. Any help is appreciated, and I am pretty new to 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:
df=pd.DataFrame.from_dict(data,orient='index')
OR
Use Transpose attribute:
df=pd.Dataframe(data).T
OR
Use transpose()
method:
df=pd.DataFrame(data).transpose()
You can use 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"
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.