How to Remove a Substring of String in a Dataframe Column?

后端 未结 2 1691
心在旅途
心在旅途 2021-01-11 21:22

I have this simplified dataframe:

ID, Date
1 8/24/1995
2 8/1/1899 :00

How can I use the power of pandas to recognize any date in the datafr

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-11 21:30

    To apply this to an entire dataframe, I'd stack then unstack

    df.stack().str.replace(r'\s:00', '').unstack()
    

    functionalized

    def dfreplace(df, *args, **kwargs):
        s = pd.Series(df.values.flatten())
        s = s.str.replace(*args, **kwargs)
        return pd.DataFrame(s.values.reshape(df.shape), df.index, df.columns)
    

    Examples

    df = pd.DataFrame(['8/24/1995', '8/1/1899 :00'], pd.Index([1, 2], name='ID'), ['Date'])
    
    dfreplace(df, '\s:00', '')
    


    rng = range(5)
    df2 = pd.concat([pd.concat([df for _ in rng]) for _ in rng], axis=1)
    
    df2
    

    dfreplace(df2, '\s:00', '')
    

提交回复
热议问题