Given the dataframe and list below -
dfx = pd.DataFrame(np.random.rand(5,10))
dfx.columns = ['id1', 'id2', 'maars1', 'maars2', 'maars3', 'maars4', 'crsc1', 'crsc2', 'crsc3', 'crsc4']
lst = ['maars', 'crsc']
How do I create 2 dataframes. The first one will be called "df_maars" and will have the following columns, "id1, id2, maars1, maars2, maars3, maars4.
The second dataframe will be called "df_crsc" and will have the following columns, "id1, id2, crsc1, crsc2, crsc3, crsc4.
Note - my dataframe and list contain way more variables than "maars" and "crsc" And each variable has about 14 columns.
You can execute the following:
df_maars = dfx[['id1', 'id2', 'maars1', 'maars2', 'maars3', 'maars4']].copy()
df_crsc = dfx[['id1', 'id2', 'crsc1', 'crsc2', 'crsc3', 'crsc4']].copy()
Try this:
df_ids = dfx.loc[:, 'id1':'id2']
df_maars = df_ids.join(dfx.loc[:, 'maars1':'maars4'])
df_crsc = df_ids.join(dfx.loc[:, 'crsc1':'crsc4'])
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.