Reputation: 161
df = pd.DataFrame(np.random.randint(0,10,size=(100,6)),columns =
['red','blue','yellow','green','purple','total'])
df_x = df[df.columns[[y,5]]]
I'd like to apply this logic to a dataframe with many more columns, but for simplicity sake, boiled down the question into this.
Below is desired output, 5 new dataframes created (Assuming I don't have to type all the 5 lines)
df_red = df[df.columns[[0,5]]]
df_blue = df[df.columns[[1,5]]]
df_yellow = df[df.columns[[2,5]]]
df_green = df[df.columns[[3,5]]]
df_purple = df[df.columns[[4,5]]]
Upvotes: 0
Views: 502
Reputation: 323276
You can do with groupby
axis =1
and pd.concat
variables = locals()
for x , y in df.iloc[:,:-1].groupby(level=0,axis=1):
variables["df_{0}".format(x)] =pd.concat([y,df.iloc[:,[-1]]],axis=1)
df_red.head()
Out[566]:
red total
0 4 7
1 7 9
2 6 7
3 4 2
4 5 8
Upvotes: 2