I have the following string which includes time and date along with \n with numbers. I want only date time value. Input:
str1 = '1 2016-04-30 00:30:00\n2 2016-04-30 02:00:00\n3 2016-04-30 02:00:00\n4 2016-04-30 03:16:00\n5 2016-04-30 08:27:18\n6 2016-04-30 10:10:00\n7 2016-04-30 10:27:00\n8 2016-04-30 13:00:00\n9 2016-04-30 14:00:00\n10 2016-04-30 16:00:00\n11 2016-04-30 16:30:00\n12 2016-04-30 16:30:00\n13 2016-04-30 17:18:00\n14 2016-04-30 19:00:00\n15 2016-04-30 19:30:00\n16 2016-04-30 22:00:00\n17 2016-04-30 23:12:00\n18 2016-04-30 23:30:00\n19 2016-04-30 23:50:00\n20 2016-04-30 23:50:00\n21 2016-04-30 23:50:00\nName: CrimeDate, dtype: datetime64[ns]'
output:
'2016-04-30 00:30:00,2016-04-30 02:00:00,2016-04-30 02:00:00,2016-04-30 03:16:00,2016-04-30 08:27:18,2016-04-30 10:10:00,2016-04-30 10:27:00,2016-04-30 13:00:00,2016-04-30 14:00:00,2016-04-30 16:00:00,2016-04-30 16:30:00,2016-04-30 16:30:00,2016-04-30 17:18:00,2016-04-30 19:00:00,2016-04-30 19:30:00,2016-04-30 22:00:00,2016-04-30 23:12:00,2016-04-30 23:30:00,2016-04-30 23:50:00,2016-04-30 23:50:00,2016-04-30 23:50:00'
I have tried the following ways to fix the problem:
str1 = str1.split(',')[0]
ini_string=' '.join(str1.split())[0:-16]
res = ini_string.replace(' ', ',')
but this is not working. Is there any better way to get the desired results. I am doing this in python 3.
pandas.DataFrametherefore you should try to usepandasoperations to perform the conversion. See my suggestion for example.