For groupby need some aggregation function(s), like mean, sum, max:
df.sort_values(['col5'],ascending=False).groupby('col1').mean().reset_index()
Or:
df.sort_values(['col5'],ascending=False).groupby('col1', as_index=False).mean()
Answer from jezrael on Stack Overflowpython 3.x - AttributeError: Cannot access callable attribute 'reset_index' of 'DataFrameGroupBy' objects, try using the 'apply' method - Stack Overflow
DatetimeIndex columns cause reset_index() to throw AttributeError
python - AttributeError: 'function' object has no attribute 'index' - Stack Overflow
python - 'numpy.ndarray' object has no attribute 'reset_index' - Stack Overflow
I have a dataframe that I've moved down to two columns 'last date read' and 'read count'. I formatted it to %Y-%m and then grouped it using the dates and summed the total amount of books read.
I wanted to plot it using month_calplot from plotly_calplot but I get the following error and I'm not sure where to go from here:
Error:
line 247, in month_calplot
gData = data.set_index(x)[y].groupby(Grouper(freq="M")).sum()
File "C:\Python\Python310\lib\site-packages\pandas\core\generic.py", line 5575, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'set_index'Code:
df2 = df2[['last date read', 'read count']]
df2['last date read'] = pd.to_datetime(df2['last date read'])
df2['last date read'] = df2['last date read'].dt.strftime('%Y-%m')
df2 = df2.groupby(['last date read'])['read count'].sum()
print(df2)
fig3 = month_calplot(
df2,
x='last date read',
y='read count',
colorscale="Purpor",
showscale=True,
total_height=250,
dark_theme=True)For groupby need some aggregation function(s), like mean, sum, max:
df.sort_values(['col5'],ascending=False).groupby('col1').mean().reset_index()
Or:
df.sort_values(['col5'],ascending=False).groupby('col1', as_index=False).mean()
You can try the below code, I had a similar issue.
grouped=data.groupby(['Colname'])
grouped.apply(lambda _df: _df.sort_values(by=['col_to_be_sorted']))
I don't think there is reset_drop in pandas, but if you want to reset the index you can use df.reset_index(drop=True).
You're trying to call a pandas function on a numpy array. You have to call it on an instance of pandas.DataFrame.
The name of the function is also reset_index and it is used as your_dataframe.reset_index(drop=True)
I don't understand... This exact code works for this same dataset look for different "X" variables, but now it is giving me this error...?
My code:
Import os Import pandas as pd
Os.chdir('file locations')
df = pd.read_csv('reading the file.csv') df = df.columns.str.replace(' ', '_')
def alphabet (row): If any(x in ['A', 'B', 'C'] for x in [row['alphanumeric_1'], row['alphanumeric_2]]): return 1 else: return 0
df['alphabet_yn] = df.apply(lambda row: alphabet (row), axis =1)
I don't understand why I am getting the AttributeError on the final line.
I am on my phone, so I apologize for how hard this might be to understand.