Home > Back-end >  Remove "_months" in a column
Remove "_months" in a column

Time:01-31

df = {'name' : ['maman','tata','papa','uncle'], 'date' : ['6_months','4_months','2_months', '2_months'], 'amount' : [2000 , 600 , 500 , 100 ]} How can i please remove the "_months" and just keep the numbers in the columns

CodePudding user response:

Code:

import pandas as pd
df = {'name' : ['maman','tata','papa','uncle'], 'date' : ['6_months','4_months','2_months', '2_months'], 'amount' : [2000 , 600 , 500 , 100 ]}
df = pd.DataFrame(df)
df['date'] = [int(x.split("_")[0]) for x in df['date']]   #It is store as an int
print(df)

Output:

    name  date  amount
0  maman     6    2000
1   tata     4     600
2   papa     2     500
3  uncle     2     100

CodePudding user response:

Hi you didnt put any code that you used it seems like your df is saved in a dict so i used some simple python looping and dict actions:

df = {'name' : ['maman','tata','papa','uncle'], 'date' : ['6_months','4_months','2_months', '2_months'], 'amount' : [2000 , 600 , 500 , 100 ]}

fixed_dates = []
for value in df['date']:
    fixed_dates.append(value.split('_')[0])
        
print(fixed_dates)
df['date'] = fixed_dates
print(df)
  • Related