1

I am quite new to python. I want to map to the latest version. If there are two versions in Software_Version column always need to choose the second one which is the latest version.

Sofware_Component     Software_Version 

Python                     2.7
Python                     2.7,3.6
R                          3.5.0,3.6.0
R                          3.5.0

Code to replace the latest version:

result4.loc[result4['COMPONENT_VERSION'].str.contains(',')] = result4['COMPONENT_VERSION'].str.split(',').str[-1]

ERROR:ValueError: cannot index with vector containing NA / NaN values

Code to map which is working fine

result4['Software_Componenet'] = result4['SOFTWARE_COMPONENT'].map(str)+' '+result4['COMPONENT_VERSION'].map(str)

I am expecting the result as below

Software_Component
Python 2.7
Python 3.6
R 3.6.0
R 3.5.0
cs95
  • 379,657
  • 97
  • 704
  • 746
PCH
  • 43
  • 5

1 Answers1

1

Extending on this thread, you can call distutils.version.LooseVersion iteratively from max inside a list comprehension:

from distutils.version import LooseVersion

[max(vers.split(','), key=LooseVersion) for vers in df['Software_Version']]
# ['2.7', '3.6', '3.6.0', '3.5.0']

df['Software_Version'] = [
    max(vers.split(','), key=LooseVersion) for vers in df['Software_Version']
]
df

  Sofware_Component Software_Version
0            Python              2.7
1            Python              3.6
2                 R            3.6.0
3                 R            3.5.0

If you fancy a more pandaic version (although this'd be slower), you can split and apply:

df['Software_Version'].str.split(',').apply(max, key=LooseVersion)

0      2.7
1      3.6
2    3.6.0
3    3.5.0
Name: Software_Version, dtype: object

To join them together into a single column, use agg:

df.agg(' '.join, axis=1)

0    Python 2.7
1    Python 3.6
2       R 3.6.0
3       R 3.5.0
dtype: object
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Thank you so much for your reply. But I am getting an error. AttributeError: 'LooseVersion' object has no attribute 'version' – PCH Jun 07 '19 at 06:35
  • @PCH This works for me in python3.6, can you tell me your python version so I can look into this? – cs95 Jun 11 '19 at 02:16