I hve a dataframe where I want to use the group by function for Region column.It works fine in data frame I am doing
import pandas as pd
#df=pd.read_csv(r'C:\Users\mobeen\Downloads\pminus.csv')
df=pd.read_csv(r'C:\Users\final.csv')
print(df)
df1=[v for k, v in df.groupby('region')]
df1
df1.to_csv('filename2',na_rep='Nan',index=False)
but after that I want to write the output in csv and it throws following error
AttributeError: 'list' object has no attribute 'to_csv' How can I write it into csv? I already checked this but it is not working
CodePudding user response:
You are calling to_csv
from the module (in pd.to_csv(df1,'filename2',na_rep='Nan',index=False)
. Call df1.to_csv('filename2',na_rep='Nan',index=False)
, as that is the actual dataframe.
CodePudding user response:
Although your code does not show you calling to_csv()
, the error suggests that you're calling the function directly on the pandas module, i.e. pd.to_csv()
. You should call the function on the dataframe, like so: df.to_csv(filename)
.