0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

well, thank you i think that could works, but i think i might be a little lost in the process, how do i call for the specific dataframe? for example if i want to process only XXX093?
If you only want to do it for some sites, add a filter to the list comprehension

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.