2

I have two dataframes:

  • One is called data, with several columns including one called bank with different banks names.
  • The other is banks and contains a list with each bank from data.

I need to create an individual dataframe for each bank from the original dataframe ("data"), iterating from the other dataframe ("banks").

I tried with something like this, but it doesn't work

for ind in banks.bank: 
     [ind] = data[(data['bank'] == [ind])]

These are samples of the data

dataframe "data" or "datos"

dataframe "banks" or "bancos"

I must divide the first dataframe for each bank of the second dataframe through the "bank" column

5
  • Can you show sample of data to understand better? Commented Nov 28, 2019 at 23:11
  • yes! of course, thanks Commented Nov 29, 2019 at 11:35
  • So you want to match dataframes bancos and datos using the index? Commented Nov 29, 2019 at 11:38
  • 1
    not exactly, I must filter the first dataframe by the bank column for each row of the second dataframe Commented Nov 29, 2019 at 12:00
  • so you want to match the 2 dataframes based on bank column and create new dataframe for each bank, right? Commented Nov 29, 2019 at 12:02

2 Answers 2

1

I hope it helps you.

#inner join 2 dataframes based on bank name
merged_inner = pd.merge(datos,bancos, on=['bank','bank'])

#dictionary of banks, key-value, key = bank name, value=details of each bank     
bank_dict = {k: v for (k, v) in merged_inner.groupby('bank')}

#print them
for i in bank_dict:
   print(bank_dict[i])
Sign up to request clarification or add additional context in comments.

Comments

0

You can store them in a dictionary and access them using the key value:

# Initialize empty dictionary
banks_dict={}

# Add each bank by looping over bank column
for b in banks['bank'].unique(): 
     banks_dict[b] = data[data['bank']==b]

then you can access a bank by its name: banks_dict[<bank_name>]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.