Answers for "add column to dataframe pandas same value"

1

Add new column based on condition on some other column in pandas.

# np.where(condition, value if condition is true, value if condition is false)

df['hasimage'] = np.where(df['photos']!= '[]', True, False)
df.head()
Posted by: Guest on August-08-2021
0

add multiple columns to dataframe if not exist pandas

cols = ['B','C','D']

df = df.reindex(df.columns.union(cols, sort=False), axis=1, fill_value=0)
print (df)
   A  B  C  D
0  1  2  3  0
1  4  4  5  0
2  1  2  3  0
3  4  4  6  0
Posted by: Guest on April-18-2021

Code answers related to "add column to dataframe pandas same value"

Python Answers by Framework

Browse Popular Code Answers by Language