how to change datatypes with mixed type

Viewed 54

I have this sample data from Kaggle https://www.kaggle.com/datasets/gregorut/videogamesales enter image description here and the sales data have mixed type. for ex, it has 0.04, 50K.... extras so it gets object as type. I want to change the -K, -M things as int and multiply 1000, 100 each. so I tried this and didn't work

what should I do?

for x in df['NA_Sales']: if type(x) is object: (x.replace('M','')) & (x*100) elif type(x) is float: x*100 preprocessing(df['NA_Sales']) df['NA_Sales'] for x in df['NA_Sales']: if type(x) is object: (x.replace('M','')) & (x*100) elif type(x) is float: x*100 preprocessing(df['NA_Sales']) df['NA_Sales']

1 Answers

The dataset from the Kaggle link that you shared in your original question seems to already contain cleaned data, but I can now see the uncleaned data that you linked in your comment (with "K", "M", object types and numerical inconsistencies).

I'm sure there is a cleaner, more Pandas-centric approach to this, but since this requires multi-conditional replacement, I took the following approach:

import pandas as pd

df = pd.read_csv(r"path_to_data\vgames2.csv")
sales_cols = df.columns[-4:]

for col in sales_cols:
    for i, r in enumerate(df[col]):
        if 'M' in df[col][i]:
            r1 = float(r.replace('M', ''))*(10**6)
            df.at[i, col] = r1
        else:
            r1 = float(r.replace('K', ''))*(10**3)
            df.at[i, col] = r1
    df[col] = pd.to_numeric(df[col])

Basically I iterated through the sales columns, and then through each row, checking whether there was an "M" or a "K" and multiplying the string accordingly by converting to float. I believe that the string containing neither a "K" nor an "M" are meant to be "thousands".

Finally, I converted each column to the numeric datatype.

If anyone has a more concise way of doing this, please feel free to share, since I curious to learn of a quicker method.

Related