To add a new column with variables to a pandas dataframe in Python, you can simply assign a list or numpy array of values to a new column name using the square bracket notation. Here's an example code snippet:
main.py219 chars12 lines
In this code, we first create a new dataframe called df
. Then, we define a variable my_var
with a list of values. Finally, we create a new column in the dataframe called C
and set its values to the my_var
variable. We can then print the dataframe to see the new column has been added with the specified values.
gistlibby LogSnag