Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Adding new column to existing DataFrame in Pandas using assign method

# import pandas library
import pandas as pd

# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]})

# print the DataFrame
print(df)

# append multiple columns to Pandas DataFrame
df2 = df.assign(lost=[2, 1, 3, 4], matches_remaining=[2, 3, 1, 1])

# Print the new DataFrame
print(df2)
Comment

How to Create new dataframe columns from existing column

new_df = df.filter(like='n_') 
           .replace(0., np.inf) 
           .apply(lambda x: sorted(x), axis=1, result_type='expand') 
           .replace(np.inf, 0.0)

new_df.columns = ['new_1', 'new_2', 'new_3']

out = pd.concat([df, new_df], axis=1)
Comment

Create a new column in Pandas DataFrame based on the existing columns

# importing pandas as pd
import pandas as pd
  
# Creating the DataFrame
df = pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'],
                    'Event':['Music', 'Poetry', 'Theatre', 'Comedy'],
                    'Cost':[10000, 5000, 15000, 2000]})
  
# Print the dataframe
print(df)
Comment

PREVIOUS NEXT
Code Example
Python :: python image heatmap 
Python :: how to remove a string in python 
Python :: login views django template passing 
Python :: python list clear vs del 
Python :: dfs algorithm python 
Python :: hash table data structure python 
Python :: python logical operators code 
Python :: convert time 
Python :: .save() in django 
Python :: sum python 
Python :: python and flask create_app 
Python :: floor function in python 
Python :: how to check a string in if statement python 
Python :: python number of specific characters in string 
Python :: set() python 
Python :: python round float to 2 decimals 
Python :: pythonanywhere django 
Python :: Interfaces 
Python :: transpose matrix python 
Python :: Python - How To Convert String to ASCII Value 
Python :: Python String index() 
Python :: Simple Kivy pong game 
Python :: custom header footer in odoo 
Python :: summarize within arcpy 
Python :: iif python 
Python :: python check if division has remainder 
Python :: useful functions in python 
Python :: éliminer le background image python 
Python :: 144/360 
Python :: auto clipping path when upload image using python 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =