Home > Enterprise >  Replace text between two characters that also appear between the characters
Replace text between two characters that also appear between the characters

Time:02-18

I loaded a CSV file using pandas in Python. It has multiple columns (ID, DATA, VALUE). The column that has information about Noise values is het one I am interested in (example of single row of the VALUE column is shown below), however, this data is accompanied with string data I don't need. I would like to only have the final value (ex. 42.963394877). I tried replace option of pandas but since the ' is present multiple times it isn't useful.

{'measurementType': 'LAeq', 'time': '2021-01-31T22:58:15.106Z', 'duration': 1000, 'value': 43.963394877}

It is exported as a CSV afterwards. If the value remains string or becomes Integer doesn't matter.

Any ideas what would be the easiest way?

CodePudding user response:

The data that you have is supposed to be stored in a dictionary. As far as I can understand your statement, you only need the data from value column. You can use the below given snippet to get the value column

df=pd.read_csv('filename.csv')
values=df['values']

This way you'll get the whole values column. If it's not what you want, can you please elaborate your problem in a better way since your problem is a bit vague.

CodePudding user response:

I see, so now you have this string "{'measurementType': 'LAeq', 'time': '2021-01-31T22:58:15.106Z', 'duration': 1000, 'value': 43.963394877}" and want the value part.

The easiest way is to convert it to dictionary using json.loads, and then fetch the value column. You can get it done by using the below given code snipper.

import json
temp='{"measurementType": "LAeq", "time": "2021-01-31T22:58:15.106Z", "duration": 1000, "value": 43.963394877}'
convertedDict = json.loads(temp)
print(convertedDict['value'])

This basically converts a string to json if its in json format and we fetch the value column in the string to get the result.

  • Related