I have a CSV with multiple values for a single value and I have to filter them out based on several conditions. Below is an example of my data.
df1 = pd.DataFrame(
data=[['Afghanistan','2.7;2.7','27.0;26.7','','22.9;22.8'],
['Bahrain','6.3;6.3;6.4','13.0;13.0;13.0','16.8;17.0',''],
['Djibouti','3.0;3.0;3.0','2.0','','23.1;24']],
columns=['Country', '2019', '2018', '2017', '2016'])
Following are the conditions to use to filter:
- if the values are duplicated, select one.
- if the values differ and the difference is less than 0.5, for eg. 26.7 and 27.0, we select 26.7 as we want to preserve the precision and would discard the rounding offs eg. 6.7 and 6.8, preserving both as both give precision. However, this contradicts the 0.5 rule, so taking any is also okay
- If the values differ and the difference is more than 0.5, select both eg. 23.1 and 24, select both
Below is my desired output for this example.
desired_op = pd.DataFrame(
data=[['Afghanistan','2.7','26.7','','22.9;22.8'],
['Bahrain','6.3;6.4','13.0','16.8',''],
['Djibouti','3.0','2.0','','23.1;24']],
columns=['Country', '2019', '2018', '2017', '2016'])
This is a small example of the dataset. To conduct this operation, I have to convert the values to numeric format first, However, the row headings (country) and column headings(year) still have to be a string. I have more than 20 columns, and more than 50 datasets so converting each column's data to numeric is also not feasible. Please Help!