user2882635
user2882635

Reputation: 163

Python Pandas replacing part of a string

I'm trying to filter data that is stored in a .csv file that contains time and angle values and save filtered data in an output .csv file. I solved the filtering part, but the problem is that time is recorded in hh:mm:ss:msmsmsms (12:55:34:500) format and I want to change that to hhmmss (125534) or in other words remove the : and the millisecond part. I tried using the .replace function but I keep getting the KeyError: 'time' error.

Input data:

time,angle
12:45:55,56
12:45:56,89
12:45:57,112
12:45:58,189
12:45:59,122
12:46:00,123

Code:

import pandas as pd

#define min and max angle values
alpha_min = 110
alpha_max = 125

#read input .csv file
data = pd.read_csv('test_csv3.csv', index_col=0)

#filter by angle size
data = data[(data['angle'] < alpha_max) & (data['angle'] > alpha_min)]

#replace ":" with "" in time values
data['time'] = data['time'].replace(':','')

#display results
print data

#write results
data.to_csv('test_csv3_output.csv')

Upvotes: 0

Views: 83

Answers (2)

NYC Coder
NYC Coder

Reputation: 7604

That's because time is an index. You can do this and remove the index_col=0:

data = pd.read_csv('test_csv3.csv')

And change this line:

data['time'] = pd.to_datetime(data['time']).dt.strftime('%H%M%S')

Output:

     time  angle
2  124557    112
4  124559    122
5  124600    123

Upvotes: 2

Daniel B
Daniel B

Reputation: 322

What would print (data.keys()) or print(data.head()) yield? It seems like you have a stray character before\after the time index string, happens from time to time, depending on how the csv was created vs how it was read (see this question).

If it's not a bigger project and/or you just want the data, you could just do some silly workaround like: timeKeyString=list(data.columns.values)[0] (assuming time is the first one).

Upvotes: 1

Related Questions