简体   繁体   中英

Replacing and inserting characters in a dateset python

I have this data set that contains asymmetry between the left and right leg. I would like to display the data as a graph and my thought process is that convert the left leg data('L') to negative values, eg -3.1, and the right leg data to positive values. I can't quite figure it out, so far I got:

df_selection['Asymmetry)'] = df_selection['Assymetry'].str.replace('R','+').replace('L','-')

This however places the + & - after the number and the R is the only thing that changes and leaves # L as it is. So the output that I want is something like this: | Asymmetry | | -------- | | 1.3 |
| 2.5 |
| -3.1 |

Here's the dataset

测试数据

I would split your single array into two arrays of left and right. If you want to make the left's negative you can still do that using this methods and just negate the numbers.

fd = ['1.3 R', '3.4l','2.5 R', ' 3.1L']
right = [float(each.upper().replace('R','').strip()) for each in fd if 'R' in each.upper()]
left = [float(each.upper().replace('L','').strip()) for each in fd if 'L' in each.upper()]

...now plot right and left in different colors on the same plot...

To do it your way:

[float(each.upper().replace('R','').strip()) if 'R' in each.upper() else -float(each.upper().replace('L','').strip()) for each in fd]

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