Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python add columns to dataframe without changing the original

#given an original pandas.DataFrame named 'df', and the data series that we want to associate with the new column named 's', 3 possible solutions:

#1-Use "assign()" to create new column and then assign values to it
df_w_new_col = df.assign (name='New_Col')

#2-create an aditional Dataframe with just the new column to add and concatenate with the old dataframe. Like a great majority of pandas methods, this actually creates a new df, it does not just concatenate to the old one so it is safe to change the old and new df's independetly
df = pd.concat( [df, pd.DataFrame({'New_Col:s})], axis=1 )

#3-explicitely create a copy and append a column
df2 = df.copy()
df2['New_Col'] = s
Comment

PREVIOUS NEXT
Code Example
Python :: python pass arguments in command line 
Python :: how to run python in the browser 
Python :: numpy roll 
Python :: bitwise xor in python 
Python :: how to make every item compare the rest items of list in python 
Python :: python ON DUPLICATE KEY UPDATE 
Python :: remove str to set in python 
Python :: time zone 
Python :: palindrom python rekursiv 
Python :: can list hold different data types in python 
Python :: Reverse an string Using Loop in Python 
Python :: for each in python 
Python :: for in loop python 
Python :: destory image in pygame 
Python :: pysimplegui get value from textbox 
Python :: normalize a group in countplot 
Python :: python gui framework 
Python :: pandas heading 
Python :: python import list from py file 
Python :: python dict items 
Python :: python tkinter programming project ideas 
Python :: python numpy how to empty array cycle 
Python :: // in python 
Python :: django login required as admin 
Python :: add vertical line in plot python 
Python :: hexdigest python 
Python :: python escape forward slash 
Python :: declaring list size python 
Python :: field in django 
Python :: full_like numpy 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =