0

I have a list of names, ['name1', 'name2',... 'nameN'], that I would like to use as names for the resulting dataframes after filtering the original dataframe by each name in a for loop

name1 = originaldf[originaldf.names = 'name1']

Is there a function in python, similar to assign in R, that I can use to accomplish this. Any other solutions are welcome.

As requested. Here is an example:

names = ['name1', 'name2', 'name3']

f = pd.DataFrame({'names' : np.random.choice(names, size=15)})
name1 = df[df.names=='name1']
name2 = df[df.names=='name2']
name3 = df[df.names=='name3']

# Then: 
display(name1, name2, name3)

names
1   name1
2   name1
3   name1
4   name1
5   name1
names
0   name2
7   name2
names
6   name3
8   name3
9   name3
10  name3
11  name3
12  name3
13  name3
14  name3
Jorge
  • 2,181
  • 1
  • 19
  • 30
  • 1
    Please provide a reproducible example with your desired output. – Sai Kumar Oct 13 '18 at 15:19
  • 3
    Perhaps consider storing them in a dictionary, with the keys as the names and values the DataFrames – ALollz Oct 13 '18 at 15:24
  • Does this answer your question? [How can you dynamically create variables via a while loop?](https://stackoverflow.com/questions/5036700/how-can-you-dynamically-create-variables-via-a-while-loop) – AMC May 10 '21 at 14:35

2 Answers2

4

I had the same problem. In Python to create a dataframe based on a name stored as a string you can use globals() like this:

name = 'name1'
globals()[name] = df

In this example, df is the copy of the dataframe you're trying to rename, so in your example that should be:

names = ['name1', 'name2', 'name3']
for n in names:
    globals()[n] = df[df.names==n]
Luc
  • 41
  • 3
1

You can use setattr for this, but i (as others) strongly recommend using a dictionary .

import sys
import pandas as pd

thismodule = sys.modules[__name__]
names = ['name1', 'name2', 'name3']

for i in names:
    setattr(thismodule, i,df[df.names==i])

display(name1,name2,name3)

This sets a dataframe to each value in names

Christian Sloper
  • 7,440
  • 3
  • 15
  • 28
  • Thanks for your answer. Can you share the reasoning for using the dictionary method over the setattr method? – Jorge Oct 17 '18 at 23:09
  • 1
    The main practical reason to have a dictionary is that it simplify questions that use many dataframes. like "What is the max value in each frame?". referencing the frames would be d["name1"] with dictionary vs name1 with setattr. Which is not much more difficult. – Christian Sloper Oct 18 '18 at 08:30
  • Thanks. I think setattr is perfect for what I am doing. – Jorge Oct 19 '18 at 12:50