Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

add columns not in place

#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

add columns not in place


In [483]: A.assign(name=s)
Out[483]:
          0         1  name
0  0.876880  0.915174     0
1  0.644664  0.305387     1
2  0.930093  0.562453     2
3  0.807626  0.498871     3

Comment

PREVIOUS NEXT
Code Example
Python :: get value of 1 field in model django 
Python :: enormous input test codechef solution 
Python :: Example of ceil method in python 
Python :: Python - How To Concatenate List of String 
Python :: Accessing elements from a Python Nested Dictionary 
Python :: ValueError: only one element tensors can be converted to Python scalars 
Python :: reload class module python 
Python :: Find Factors of a Number Using Function 
Python :: python sort list opposite 
Python :: return function in python 
Python :: python module has no attribute 
Python :: 2nd to last index python 
Python :: how to get django 
Python :: python tutorial 
Python :: Iterating With for Loops in Python 
Python :: python get module name 
Python :: fix the debug_mode = false django 
Python :: delete content of table django 
Python :: keep the user logged in even though user changes password django 
Python :: difference between this and super 
Python :: python MAX_INT 
Python :: convert hex rgb to matplotlib color 
Python :: python destructuring 
Python :: python regeression line 
Python :: python calendar table view 
Python :: Async-Sync 
Python :: check if value is in series pandas 
Python :: how to make loops in python 
Python :: euclidean distance 
Python :: how to find the last occurrence of a character in a string in python 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =