I have a dataset like this:
match one ics two
0 Juventus\n•\nSassuolo 1\n1.46 X\n5.25 2\n7.5
1 Sampdoria\n•\nAtalanta 1\n4.33 X\n4 2\n1.87
2 Udinese\n•\nHellas Verona 1\n2.52 X\n3.35 2\n3.1
3 Cagliari Calcio\n•\nRoma 1\n5.8 X\n4.33 2\n1.62
4 Empoli\n•\nInter 1\n7.2 X\n5.25 2\n1.47
I would like to get rid of the characters before and equal to n to convert the column one ics and two into float numbers. I do like this but it doesn't work
for index, row in matches.iterrows():
matches.loc[index, 'one'] = matches.loc[index, 'one'].split('n', 1)[-1]
matches.loc[index, 'ics'] = matches.loc[index, 'ics'].split('n', 1)[-1]
matches.loc[index, 'two'] = matches.loc[index, 'two'].split('n', 1)[-1]
matches['one'] = matches['one'].astype(float)
matches['ics'] = matches['ics'].astype(float)
matches['two'] = matches['two'].astype(float)
CodePudding user response:
If I understand your question, try:
temp = '2\n7.5'
temp1 = temp.split('\n')
print(temp1[0], ",", temp1[1], "or", temp1)
output:
2 , 7.5 or ['2', '7.5']
CodePudding user response:
Try with str.split
and astype
:
>>> df.set_index("match").apply(lambda x: (x.str.split().str[-1]).astype(float))
one ics two
match
Juventus\n•\nSassuolo 1.46 5.25 7.50
Sampdoria\n•\nAtalanta 4.33 4.00 1.87
Udinese\n•\nHellas Verona 2.52 3.35 3.10
Cagliari Calcio\n•\nRoma 5.80 4.33 1.62
Empoli\n•\nInter 7.20 5.25 1.47