-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
Closed
Labels
Description
We often find ourselves checking for column existence before assigning a value to it, e.g.:
df = pd.DataFrame()
col = 'ColumnA'
val = 'Value1'
if col not in df.columns:
df[col] = val
And we were thinking it would be nice if there was a way to tell pandas: add this column with that value(s) but only if it does not exist already - somewhat similar to dict.setdefault() so we could say, for instance:
df.setdefault(col, val)
Doesn't necessarily have to be that syntax obviously - just a suggestion. What do you guys think?
lrq3000, ankostis, MelodyKnit and stuarteberg