Reputation: 4767
Is it possible to chain the following three statements together into one?
raw_df = df.copy().astype({'ids': 'str'})
raw_df['dels'] = np.where(raw_df['dels'] == 'nan', None, raw_df['dels'])
raw_df['langs'] = np.where(raw_df['langs'] == 'nan', None, raw_df['langs'])
If so, how could that be done?
Upvotes: 1
Views: 96
Reputation: 13417
You can use the assign
method to create new columns/overwrite existing columns. You can also shorten the logic here from np.where
via Series.mask
(or Series.replace
) since all you want to do is swap our 'nan'
for None
and keep the rest of the column untouched.
Without seeing a copy of your data, I can't be certain this will work but you can try:
new_df = (raw_df.copy()
.astype({"ids": "str"})
.assign(
dels=lambda df: df["dels"].mask(df["dels"] == 'nan', None),
langs=lambda df: df["langs"].mask(df["langs"] == 'nan', None))
)
Alternatively, if you just want to go ahead and replace 'nan'
for None
throughout the whole dataframe (and not just 2 columns) you can do it like this:
new_df = (raw_df.copy()
.astype({"ids": "str"})
.replace("nan", None))
Upvotes: 4