简体   繁体   中英

Remove First and Last value from list in pandas dataframe Column

my data frame looks like this.

数据框

0   abc 2019-02-12 11:03:32 PM 12345
1   def 2019-02-14 11:04:33 PM 12345
2   blank
3   ghi 2019-02-14 11:05:34 PM 12345

Wanted output the below format.

0   2019-02-12 11:03:32 PM
1   2019-02-14 11:04:33 PM
2   
3   2019-02-13 11:05:36 PM

In short wanted to remove 1st and last values from each row and keep only date and time value.

If need remove first and last values separated by spaces use Series.str.split with indexing by str[1:-1] and then Series.str.join :

df['A'] = df.A.str.split().str[1:-1].str.join(' ')
print (df)
                        A
0  2019-02-12 11:03:32 PM
1  2019-02-14 11:04:33 PM
2
3  2019-02-14 11:05:34 PM

Or:

df['A'] = df.A.apply(lambda x: ' '.join(x.split()[1:-1]))

Use

df['A'] = df['A'].str.split().str[1:-1].str.join(" ")

which gives:

                        A
0  2019-02-12 11:03:32 PM
1  2019-02-14 11:04:33 PM
2
3  2019-02-14 11:05:34 PM

The first 1:3 slice index 0 and index 3 and index 1 and 2 would be printed


df = df.iloc[:, 1:2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM