Adding blank or user-initialized columns

To add a new column of a DataFrame, you can follow the name of the DataFrame with the name of the new column (enclosed in single quotes and square brackets) and set it equal to whatever value you like. To add a column of empty strings or integers, you can set the column equal to "" or numpy.nan, respectively (the latter requires importing numpy beforehand). To add a column of zeros, set the column equal to 0. The following examples illustrate these points:

df['new_col1'] = ""
df['new_col2'] = 0
print(df)

The output is as follows:

   col1  col2 col3 new_col1  new_col2
0     1     4    x                  0
1     2     5    y                  0
2     3     6    z                  0

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset