TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas Concatenation

Pandas Concatenation with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas Concatenation

Pandas is capable of combining Series, DataFrame, and Panel objects through different kinds of set logic for the indexes and the relational algebra functionality.

The concat() function is responsible for performing concatenation operation along an axis in the DataFrame.

Syntax:

pd.concat(objs,axis=0,join='outer',join_axes=None,
ignore_index=False)

Parameters:

  • objs: It is a sequence or mapping of series or DataFrame objects.
    If we pass a dict in the DataFrame, then the sorted keys will be used as the keys<.strong> argument, and the values will be selected in that case. If any non-objects are present, then it will be dropped unless they are all none, and in this case, a ValueError will be raised.
  • axis: It is an axis to concatenate along.
  • join: Responsible for handling indexes on another axis.
  • join_axes: A list of index objects. Instead of performing the inner or outer set logic, specific indexes use for the other (n-1) axis.
  • ignore_index: bool, default value False
    It does not use the index values on the concatenation axis, if true. The resulting axis will be labeled as 0, ..., n - 1.

Returns

A series is returned when we concatenate all the Series along the axis (axis=0). In case if objs contains at least one DataFrame, it returns a DataFrame.

Example1:

import pandas as pd
a_data = pd.Series(['p', 'q'])
b_data = pd.Series(['r', 's'])
pd.concat([a_data, b_data])

Output

0       p
1       q
0       r
1       s
dtype: object

Example2: In the above example, we can reset the existing index by using the ignore_index parameter. The below code demonstrates the working of ignore_index.

import pandas as pd
a_data = pd.Series(['p', 'q'])
b_data = pd.Series(['r', 's'])
pd.concat([a_data, b_data], ignore_index=True)

Output

0       p
1       q
2       r
3       s
dtype: object

Example 3: We can add a hierarchical index at the outermost level of the data by using the keys parameter.

import pandas as pd
a_data = pd.Series(['p', 'q'])
b_data = pd.Series(['r', 's'])
pd.concat([a_data, b_data], keys=['a_data', 'b_data'])

Output

a_data    0      p
          1      q
b_data    0      r
          1      s
dtype: object

Example 4: We can label the index keys by using the names parameter. The below code shows the working of names parameter.

import pandas as pd
a_data = pd.Series(['p', 'q'])
b_data = pd.Series(['r', 's'])
pd.concat([a_data, b_data], keys=['a_data', 'b_data'])
pd.concat([a_data, b_data], keys=['a_data', 'b_data'],
names=['Series name', 'Row ID'])

Output

Series name   Row ID
a_data         0    p
               1    q
b_data         0    r
               1    s
dtype: object

Concatenation using append

The append method is defined as a useful shortcut to concatenate the Series and DataFrame.

Example:

import pandas as pd
one = pd.DataFrame({
   'Name': ['Parker', 'Smith', 'Allen', 'John', 'Parker'],
   'subject_id':['sub1','sub2','sub4','sub6','sub5'],
   'Marks_scored':[98,90,87,69,78]},
   index=[1,2,3,4,5])
two = pd.DataFrame({
   'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
   'subject_id':['sub2','sub4','sub3','sub6','sub5'],
   'Marks_scored':[89,80,79,97,88]},
   index=[1,2,3,4,5])
print (one.append(two))

Output

     Name      subject_id     Marks_scored
1    Parker     sub1           98
2    Smith      sub2           90
3    Allen      sub4           87
4    John       sub6           69
5    Parker     sub5           78
1    Billy      sub2           89
2    Brian      sub4           80
3    Bran       sub3           79
4    Bryce      sub6           97
5    Betty      sub5           88





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf