C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Pandas ConcatenationPandas 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:
ReturnsA 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 appendThe 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
Next TopicPandas Data operations
|