For comparison == is needed.
EDIT:
There are another approaches. For example to use df.astype() and df.select_dtypes(). Maybe it's overly complex but still.
Below is df with three columns, two of them are int64 and one is object:
- select columns by datatype using
- to get column names with specified type
- use column names for creating dictionary for desired mapping
Now we have constructed desired mapping and can apply it to df.astype():
EDIT:
There are another approaches. For example to use df.astype() and df.select_dtypes(). Maybe it's overly complex but still.
Below is df with three columns, two of them are int64 and one is object:
>>> df = pd.DataFrame({'nums': range(1, 4), 'more_nums': range(11, 14), 'chars': [*'abc']}) >>> print(df['nums'].dtype, df['more_nums'].dtype, df['chars'].dtype)) int64 int64 objectWe want to use df.astype() to change dtype of columns based on columns current dtype. In order to create mapping needed for df.astype() we can:
- select columns by datatype using
df.select_dtypes(include='int64')
- to get column names with specified type
df.select_dtypes(include='int64').columns
- use column names for creating dictionary for desired mapping
dict.fromkeys(df.select_dtypes(include='int64').columns, 'object')
Now we have constructed desired mapping and can apply it to df.astype():
>>> df = df.astype(dict.fromkeys(df.select_dtypes(include='int64').columns, 'object'))We can check whether it worked:
>>> print(df['nums'].dtype, df['more_nums'].dtype, df['chars'].dtype)) object object object
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.