1

Is it possible to create a new dataframe in python based on concatenated strings. I have a loop and need to set up several dataframes a the output.

So in short is there a way of getting the below to work?

a='Blue'
b='Green'

Desired output:

BlueGreen=pd.DataFrame()

or 

a+b=pd.DataFrame()
fred.schwartz
  • 2,023
  • 4
  • 26
  • 53
  • Possible duplicate of [Dynamically set local variable](https://stackoverflow.com/questions/8028708/dynamically-set-local-variable) – EliadL Oct 31 '19 at 13:12

2 Answers2

2

Dangerous Way:

exec('%s = %s' % (a+b, pd.DataFrame()))

Safe Way:

d = {}
d[a+b] = pd.DataFrame()

Just you have to do:

print(d['BlueGreen'])

to get the value but you can make it to be accessed regularly, like:

locals().update(d)

Than you're set.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • Thanks great thanks. I think I'll go for the dangerous way, as its less code and I can follow whats going on better. Thanks alot – fred.schwartz Oct 31 '19 at 12:13
1

hmmm, I'm not sure if I'm getting it right or no!? u can easily concatenate(add them to each other) strings and change it to dataframe like this:

//if color is the name of your list of string
d={'input ur header name here': color}
d=pd.DataFrame(data=d)

if i got it wrong, please describe your problem more

parsa
  • 370
  • 4
  • 13