0

I have a column with different type of units and want to convert them to the same unit, some of the record have "Varies with device" which I want to keep without touching them

input:

df1["Size"]

output

0                       19M
1                       14M
2                      8.7k
3                       25M
4                      2.8M
                ...        
10834                  2.6M
10836                   53k
10837                  3.6M
10839    Varies with device
10840                   19M

I tried to use this code but it does not working!

def convert_size(x):
    if x[-1] == "M":
        return str(x[:-1])*1000 
    
    else: return x 
    
    df1["Size"] = df1["Size"].apply(convert_size)

note: 1000k = 1M

Kizen
  • 1
  • 1
    Does this answer your question? [Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe](https://stackoverflow.com/questions/39684548/convert-the-string-2-90k-to-2900-or-5-2m-to-5200000-in-pandas-dataframe) – Henry Ecker Apr 05 '21 at 04:44
  • A little correction. Change `str(x[:-1])*1000` to `int(x[:-1])*1000`. You are multiplying string to a number. – Parampreet Rai Apr 05 '21 at 05:39
  • I have to keep the term "Varies with device" in its place, I can't make it an integer! – Kizen Apr 05 '21 at 18:10

0 Answers0