DataFrame has no attribute group. However, it is possible to access data in a column in your dataframe with the same syntax used to access attributes and methods, i.e. if you have a column col, you may access the series related to this column through
df.col
What happened here is that your data is probably different from what she used in the tutorial. Or at least, the columns she has are different than the columns you have.
To solve that problem, you can either (I) simply rename your columns to match the columns from the tutorial or (II) replace data.group with the corresponding column name that you have in your df
I 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!
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
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%%')