Home > Back-end >  How do I remove data after a comma within a column of a dataframe using pandas?
How do I remove data after a comma within a column of a dataframe using pandas?

Time:07-28

I have a dataframe which has the below columns

df3
    Deposit Name Initial Date 
    22      Gu    03/22/2007 0:00
    30       Pa   09/30/2009 0:00
    11       Ch   1/15/22, 5/11/21

the problem is for row 3, I want the initial date only before the comma seperated. So my final df should not have " , 5/11/21".

Im able to manually do this by

df3.at[3,'Initial Date']= "1/15/22"

But I want a permanent solution so that if there is ever an occurrence in the future where there are more than 1 date under Initiate Date column, then pandas only stores the first date before the comma. How do I do this?

CodePudding user response:

df3['Initial Date'].replace(",.*", "", regex=True, inplace=True)
  • Related