Home > OS >  How to split dataframe into multiple dfs, with special characters such as ' . ' , '-&
How to split dataframe into multiple dfs, with special characters such as ' . ' , '-&

Time:09-20

I'm trying to separate my data into into multiple dataframes.

It didn't work because '.com', '.us', '.de', '.in' etc. domains are not being recognized by python.

Please suggest a way to

I converted groupby object to tuples and then to dict.

d = dict(tuple(df.groupby('Site')))
for i, g in df.groupby('Site'):
    globals()['df_'   str(i)] =  g

print (df_xyz.com)

Error message says:

AttributeError: 'DataFrame' object has no attribute 'xyz'

What I'm trying to achieve here is, to export these mutiple dataframes into a workbook with multiple sheets like xyz.com , xyz.us, xyz.in etc.

CodePudding user response:

Perhaps the error is that you are attempting to create a variable with '.' in the name, which is not allowed in Python. May I suggest:

d = dict(tuple(df.groupby('Site')))
for i, g in df.groupby('Site'):
    globals()['df_'   str(i).replace('.','_')] =  g

print (df_xyz_com)
  • Related