i seems cant find the right answer after spending long time searching for the correct problem here
so, i got
sites = df_sim1['site_id'].unique()
['XXX092' 'XXX093']
i'm using it to create 2 dataframes from 1 big dataframe
for site in sites:
exec(f"{site} = df_sim1[df_sim1['site_id']==site].reset_index(drop=True)")
f"{site} = {site}.drop(['site_id'], axis=1)"
and got 2 dataframes XXX092, XXX093
so I need to remove the column 'site_id', i'm thinking about looping it again
for site in sites:
for column in site.columns:
if "site_id" in column:
site.drop(column, axis = 1, inplace=True)
i got error "AttributeError: 'str' object has no attribute 'columns'"
if i'm defining the sites as
sites = [XXX092, XXX093]
i got my desired result which column 'site_id' removed from all my dataframe, any clue where do I go wrong?
CodePudding user response:
You error comes from the fact that site
is a string, the way you define it (values in unique sites), and you are calling looking for a columns
attribute inside of it which it does not have as it is not a dataframe.
I think what you want is:
[site_df.drop(columns='site_id') for site, site_df in df_sim1.groupby('site_id')]
which would give a list of dataframe, partitioning thedf_sim1
into site_id
unique values, and dropping the value of site_id
in each.