简体   繁体   中英

Last occurence of comma in python dataframe

please help me on replace comma with & in the last occurence of comma

DF['MSG'] =

0 20.00, 20.00 1 4.00, 3.00, 2.00 2 100.00 3 10.00, 70.00, 10.00 4 10.00, 10.00, 10.00, 10.00, 10.00 5 99.00 6 50.00, 50.00 7 70.00 8 10.00, 20.00, 65.00

output is:

0 20.00, 20.00 1 4.00, 3.00& 2.00 2 100.00 3 10.00, 70.00& 10.00 4 10.00, 10.00, 10.00, 10.00& 10.00 5 99.00 6 50.00, 50.00 7 70.00 8 10.00, 20.00& 65.00

if the comma occurences are more than 2 then expected in above output in dataframe plese help me

Assuming it's a clean list of numbers, you can change it to a string like this:

list_of_numbers = [1, 2, 3, 4]
print(', '.join([str(i) for i in list_of_numbers[:-1]]) + f" & {list_of_numbers[-1]}")

gives

1, 2, 3 & 4

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