Home > Blockchain >  I have a dataframe column with the values and i only want one digit in them
I have a dataframe column with the values and i only want one digit in them

Time:02-04

03/Sep/2009:21:30:27 -0700

04/Sep/2009:02:57:16 -0700

22/Aug/2009:12:58:27 -0700

One of my dataframe column have these values and I only want from -0700 I only want the 7 and some of them are not 7 i only want this anyway i can get it?

CodePudding user response:

import pandas as pd

my_list = [
    '03/Sep/2009:21:30:27 -0700',
    '04/Sep/2009:02:57:16 -0700',
    '22/Aug/2009:12:58:27 -0700']

my_df = pd.DataFrame(data=my_list)

for row in my_df.iterrows():
    print (f'3rd last digit: {row[1].values[0][-3]}')

Output:

3rd last digit: 7
3rd last digit: 7
3rd last digit: 7

CodePudding user response:

You can simply slice with str like so :

df["new"] = df["col"].str[-4:].str.strip("0").astype(int)

Output :

print(df)

                          col  new
0  03/Sep/2009:21:30:27 -0700    7
1  04/Sep/2009:02:57:16 -0700    7
2  22/Aug/2009:12:58:27 -0700    7

CodePudding user response:

import pandas as pd

df = pd.DataFrame({'fmt_dat':['03/Sep/2009:21:30:27 -0700',
                                '04/Sep/2009:02:57:16 -0700',
                                '22/Aug/2009:12:58:27 -0700'],})

df['new']=[s.split('-').pop().strip('0') for s in df['fmt_dat']]
>>> df
                      fmt_dat new
0  03/Sep/2009:21:30:27 -0700   7
1  04/Sep/2009:02:57:16 -0700   7
2  22/Aug/2009:12:58:27 -0700   7
  • Related