Reputation: 11
I have a dictionary data like below
dictData = [
{'period': '2020-01-01', 'ratio': 83.83458},
{'period': '2020-01-02', 'ratio': 81.95488},
{'period': '2020-01-03', 'ratio': 100},
{'period': '2020-01-04', 'ratio': 77.25563},
{'period': '2020-01-05', 'ratio': 90.22556}
]
... and I want really to get period
and ratio
values without iteration.
for i in list(range(0, 4)):
dictData[i]['ratio'], dictData[i]['period']
Is there any function in Python? Thank U
If period
long, it takes too long times.
Upvotes: 0
Views: 55
Reputation: 11238
if you just want to use function and not any loop thing like for
, while
you can use pandas
for this task
>>> import pandas as pd
>>> data = [{'period': '2020-01-01', 'ratio': 83.83458},
... {'period': '2020-01-02', 'ratio': 81.95488},
... {'period': '2020-01-03', 'ratio': 100},
... {'period': '2020-01-04', 'ratio': 77.25563},
... {'period': '2020-01-05', 'ratio': 90.22556}]
>>>
>>> df = pd.DataFrame(data)
>>> df
period ratio
0 2020-01-01 83.83458
1 2020-01-02 81.95488
2 2020-01-03 100.00000
3 2020-01-04 77.25563
4 2020-01-05 90.22556
>>> df['period']
0 2020-01-01
1 2020-01-02
2 2020-01-03
3 2020-01-04
4 2020-01-05
Name: period, dtype: object
>>> df['ratio']
0 83.83458
1 81.95488
2 100.00000
3 77.25563
4 90.22556
Name: ratio, dtype: float64
>>>
>>>
>>>
>>> df['ratio'].to_list()
[83.83458, 81.95488, 100.0, 77.25563, 90.22556]
>>>
>>> df['period'].to_list()
['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05']
>>>
Upvotes: 1