Proki
Proki

Reputation: 27

Iterate through Dictionary of DataFrames

I've got pretty stuck on the basic idea of DataFrames within dictionaries.

Could someone please explain to me how they would iterate through a dict and select specific columns in a DataFrame while still keeping in mind time complexity?

For that reason I created a reproducible example:

import numpy as np
import pandas as pd

data = np.random.randint(5, 30, size=(10,3))
df_APPL = pd.DataFrame(data, columns=['Volume', 'Open', 'Close'])
df_TSLA = pd.DataFrame(data, columns=['Volume', 'Open', 'Close'])
df_AMZN = pd.DataFrame(data, columns=['Volume', 'Open', 'Close'])

d = {'APPL': df_APPL,
     'TSLA': df_TSLA,
     'AMZN': df_AMZN}

In this basic example, how would I select 'Open' in every DataFrame and sort the keys from max to min according to 'Open'?

Any other example would also be welcome. :)

Upvotes: 0

Views: 684

Answers (1)

Prakash Dahal
Prakash Dahal

Reputation: 4875

You can use sort_values() to sort dataframe based on a column values:

see more about sort_values() here

nd = {k:v.sort_values('Open', ascending=False) for k,v in d.items()}

Upvotes: 2

Related Questions