You can remove your for loop and do this instead:
Copydata = df.groupby(['ta', 'tb', 'tc'])['income'].sum()
Then plot it.
Answer from Mayank Porwal on Stack OverflowI get the error " AttributeError: 'str' object has no attribute 'groupby' " when I run the following piece of code:
def groupby(df):
median_df = df.groupby('Brand').median()
return median_df
median_customer_df = groupby('customer_df')
However when outside of the function I simply run:
median_df = customer_df.groupby('Brand').median()
the code runs fine?
Can anyone please help explain why this groupby is failing only when in my function?
Thanks in advance!
python - don't know why: AttributeError: 'list' object has no attribute 'groupby' - Stack Overflow
python - AttributeError: 'DataFrame' object has no attribute 'group_by' - Stack Overflow
AttributeError: SeriesGroupBy object has no attribute ffill
python 3.x - Pandas groupby => AttributeError: 'function' object has no attribute 'mean' - Stack Overflow
You can remove your for loop and do this instead:
Copydata = df.groupby(['ta', 'tb', 'tc'])['income'].sum()
Then plot it.
Use df.groupby(i) with plt.figure() for 3 separately pie graphs:
Copya = ['ta', 'tb', 'tc']
for i in a:
d = df.groupby(i)['income'].sum()
plt.figure()
d.plot.pie(autopct='%.1f%%')
Hello,
Has anyone ever come across this before?
I'm trying to group some data in a dataframe and getting this error. The steps I've taken are:
-
in a for loop:
read in a csv from an api using pd.read_csv() replaced some values in a column using a for loop and .loc[] appended the resulting data frame to a list
2) concatenated the list of dataframes using pd.concat()
3) added a calculated column to the new DF by multiplying another column
4) added two empty columns
5) filtered the DF using .loc[] based on a value within a column
6) filtered the DF using .loc[] based on a value in a different column
7) tried to use this code:
new_DF = old_df.group_by(['col1', 'col_2', 'col_3', 'adgroup', 'col_4', 'col5', 'col6'], as_index=False)[['col7', 'col8', 'col9']].sum()
The DF seems to behaving normally for example I can do dtypes and columns on it and add columns which are calculated from other columns. What is super frustrating is that I can do pd.to_csv() and then pd.read_csv() on the DF and then I'm able to do the grouping I want (however this isn't ideal which is why I'm posting).
Any advice would be appreciated.
Cheers