How to Append Two Data Frames | Data Frame | Pandas | Pandas Tutorial

Publicado em: 25 Outubro 2021
no canal de: Parag Dhawan
419
6

To append two data frames in Python, you can use the concat() function from the pandas library. The concat() function combines two or more data frames into a single data frame by stacking them vertically or horizontally.

Here's an example of how to use concat() function to append two data frames vertically:

import pandas as pd

create two data frames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [4, 5, 6], 'B': [7, 8, 9]})

append two data frames vertically
df_concat = pd.concat([df1, df2], ignore_index=True)

print(df_concat)
In this example, we create two data frames df1 and df2 with the same column names and append them vertically using concat() function with ignore_index=True parameter to reset the index of the concatenated data frame.

To append two data frames horizontally, you need to set the axis parameter of concat() function to 1. Here's an example:

import pandas as pd

create two data frames
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'C': [7, 8, 9], 'D': [10, 11, 12]})

append two data frames horizontally
df_concat = pd.concat([df1, df2], axis=1)

print(df_concat)
In this example, we create two data frames df1 and df2 with different column names and append them horizontally using concat() function with axis=1 parameter to stack them side by side.

code snippet is given below

import pandas as pd
import numpy as np

df = pd.DataFrame([[1,2],[3,4]],columns = ['a','b'])
df2 = pd.DataFrame([[5,6],[7,8]],columns=['a','b'])

print (df)

print(df2)

df3 = df.append(df2)
print(df3)

df4 = df.append(df2,ignore_index=True)
print(df4)

‪@ParagDhawan‬
=============================================================================

Link for Tutorial Series

Jupyter Notebook Tutorial Series:-
   • How To Open Jupyter Notebook in Windows  

Python Tutorial Series:-
   • Introduction to Python | Python Appli...  

Python Assignments and Objective Questions:-
   • Objective Questions Python - 1  

Tech. Videos By Parag Dhawan;-
   • Template Matching Using OpenCV (Pytho...  

Object-Oriented Programming in Python:-
   • How to Create Class and Object in Python  

File Handling in Python:-
   • How to Create a file in Python | Pyth...  

Exception Handling in Python:-
   • Exception Handling in Python  

NumPy Tutorial Series:-
   • NumPy  


=============================================================================
Feel free to connect and ask your queries:-

Linkedin:-   / parag-dhawan  
Youtube:-    / paragdhawan  
Facebook Page:- http://fb.me/dhawanparag
Instagram: -   / paragdhawan  
Twitter:-   / dhawan_parag  
GitHub:- https://github.com/paragdhawan/

=============================================================================
Show your support by Subscribing to the channel:-

https://www.youtube.com/c/ParagDhawan...
=============================================================================

#ParagDhawan
#Pandas
#DataScience
#DataAnalysis
#PandasTutorial
#PandasCourse
#Python3
#Python
#PythonProgramming

============================================================
How to Record Your Screen and make a tutorial video or demo video: -
   • How to Record Screen and Edit Video U...  
============================================================


Nesta página do site você pode assistir ao vídeo on-line How to Append Two Data Frames | Data Frame | Pandas | Pandas Tutorial duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Parag Dhawan 25 Outubro 2021, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 419 vezes e gostou 6 espectadores. Boa visualização!