简体   繁体   中英

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

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())

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM