Home > Software design >  Is there any function to check the location of the last date element in pandas dataframe?
Is there any function to check the location of the last date element in pandas dataframe?

Time:12-19

The sample data is like:

Empty header
'2/01/2011'
'3/01/2011'
'4/01/2011'
'5.222'
'6.214'
'1.34266'

The data above are all strings.

My expected outcome is

Date Value
2/01/2011 5.222
3/01/2011 6.214
4/01/2011 1.34266

The 'Date' variable should be in date format, 'Value' variable is in float.

CodePudding user response:

Example

s = pd.Series(['2/01/2011', '3/01/2011', '4/01/2011', '5.222', '6.214', '1.34266'])

s

0    2/01/2011
1    3/01/2011
2    4/01/2011
3        5.222
4        6.214
5      1.34266
dtype: object

Code

cond1 = s.str.contains(r'\d /')
out = (s.groupby(cond1, sort=False).apply(lambda x: pd.Series(list(x)))
       .unstack(level=0).set_axis(['Date', 'Value'], axis=1))

out

    Date        Value
0   2/01/2011   5.222
1   3/01/2011   6.214
2   4/01/2011   1.34266
  • Related