Search code examples
pandasdataframenew-operator

Creating new new data frame with so many columns from existing dataframe by dropping one column


I have one dataframe called df and I need to drop one column amount from df and assign it to a new data frame called df1 but I get an error below. How can I fix this? Please note that below is a small example. In my real dataset I do have 28 columns and I can't type their names to create a new one Thank you.

df=  [['A',100,10000]
    ,['A',120,15000]
    ,['A',300,50000]
    ,['B',100,180000]
    ,['B',80,200000]]


df = pd.DataFrame(df, columns = ['group','size','amount']) 
df1=df.drop('amount',axis=1,inplace=True)
df1.head() 

"AttributeError: 'NoneType' object has no attribute 'head'"


Solution

  • inplace operations return None, so need omit assign back:

    df.drop('amount',axis=1,inplace=True)
    df.head()
    

    Or remove inpalce=True and assign back:

    df = df.drop('amount',axis=1)
    df.head()