Home > Software engineering >  how to remove the contents of a column without deleting the column in pandas
how to remove the contents of a column without deleting the column in pandas

Time:10-21

I am trying to delete the contents of a column but would like to keep the column.

For instance I have a table like.

Numbers1 Numbers2 Numbers3 Numbers4 Numbers5
five four three two two
six seven eight nine ten
nine seven four two two
seven six five three one

I would like to remove all the contents of column b but I want to keep column Numbers2

the desired output be like

Numbers1 Numbers2 Numbers3 Numbers4 Numbers5
five three two two
six eight nine ten
nine four two two
seven five three one

kindly help Thankyou

CodePudding user response:

Simply assign an "empty" value (NaN, empty string, None, whatever you want):

df['Numbers2'] = float('NaN')

output:

  Numbers1  Numbers2 Numbers3 Numbers4 Numbers5
0     five       NaN    three      two      two
1      six       NaN    eight     nine      ten
2     nine       NaN     four      two      two
3    seven       NaN     five    three      one

With empty string:

df['Numbers2'] = ''

  Numbers1 Numbers2 Numbers3 Numbers4 Numbers5
0     five             three      two      two
1      six             eight     nine      ten
2     nine              four      two      two
3    seven              five    three      one

CodePudding user response:

try:

df['Numbers2']=''  #empty string

df['Numbers2']=np.nan #nan

CodePudding user response:

update it with a empty string

df['Numbers2']=""

CodePudding user response:

First, you could delete the column with df = df.drop('Numbers2', axis=1)

Second, replace the column with df['Numbers2'] = ""

  • Related