Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

concat columns pandas dataframe

In [1]: df1 = pd.DataFrame(
   ...:     {
   ...:         "A": ["A0", "A1", "A2", "A3"],
   ...:         "B": ["B0", "B1", "B2", "B3"],
   ...:         "C": ["C0", "C1", "C2", "C3"],
   ...:         "D": ["D0", "D1", "D2", "D3"],
   ...:     },
   ...:     index=[0, 1, 2, 3],
   ...: )
   
In [8]: df4 = pd.DataFrame(
   ...:     {
   ...:         "B": ["B2", "B3", "B6", "B7"],
   ...:         "D": ["D2", "D3", "D6", "D7"],
   ...:         "F": ["F2", "F3", "F6", "F7"],
   ...:     },
   ...:     index=[2, 3, 6, 7],
   ...: )
   ...: 

In [9]: result = pd.concat([df1, df4], axis=1)
# This will merge columns of both the dataframes
Comment

Adding new column to existing DataFrame in Pandas using concat 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)

# create a new DataFrame
df2 = pd.DataFrame([[1, 2], [2, 1], [3, 4], [0, 3]],
                   columns=['matches_left', 'lost'])

# concat and Print the new DataFrame
print(pd.concat([df, df2], axis=1))
Comment

PREVIOUS NEXT
Code Example
Python :: how to negate a boolean python 
Python :: flatten list 
Python :: dict column to be in multiple columns python 
Python :: python list index() 
Python :: python fill string with spaces to length 
Python :: python treemap example 
Python :: how to give float till 5 decimal places 
Python :: convert python list to pyspark column 
Python :: pytorch dill model save 
Python :: ip address finder script python 
Python :: python recognize every white color 
Python :: list to dictionary 
Python :: how to configure a button in python tkinter 
Python :: how to change the name of a variable in a loop python 
Python :: tensorflow evaluation metrics 
Python :: plot matrix as heatmap 
Python :: numpy python 3.10 
Python :: import matplotlib pyplot as plt 
Python :: python string starts with any char of list 
Python :: discord.py read custom status 
Python :: Progress Bars in Python 
Python :: learn basic facts about dataframe | dataframe info 
Python :: create new dataframe from existing data frame python 
Python :: pandas compare two columns of different dataframe 
Python :: how to run a command in command prompt using python 
Python :: Python Regex Backslash “” 
Python :: seaborn and matplotlib python 
Python :: pandas list comprehension 
Python :: how to submit two forms in django 
Python :: tkinter change button color smoothly 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =