How To Slice A DataFrame by column name - and rename the new dataframe by iteration

Viewed 40

This is one of my first post on stack overflow and I have started to code a few months ago, therefore I am sorry if I am doing something wrong! I have looked over the web for days now but couldn't find an answer.

I am in the process of sorting through a large amount of data. The data is collected in a large data frame (fig)Alldata.

The goal is to split the dataframe into two new sub dataframes by columns name. This is achieved, however the name LocationId is a bit comprehensive and would prefer to change it to dfm + iteration number.

First part, divides dataframe by name, it works as it should, and gives dataframes as shown in Dataframes -

for name in Alldata['LocationId'].unique():
     locals()['dfM_' + name] = Alldata[(Alldata.LocationId == name)]
 

How do I do it most optimally? Have tried the below code, but have not succeeded in getting the name changed as desired.

for name in Alldata['LocationId'].unique():
     locals()['dfM_' + str(i + 1)] = for i in range(len( Alldata['LocationId'].unique())
1 Answers

I solved the problem myself, thought I would write how I solved the problem.

for idx, name in enumerate(Alldata['LocationId'].unique()):
     globals()['dfM_' + str(idx+1)] = Alldata[(Alldata.LocationId == name)]
Related