Merging & Concatenating

Here’s a quiz with 10 questions (including code-based ones) on merging and concatenating in Pandas. Each question includes options, the correct answer, and a justification.

Last Updated :
Discuss
Comments

Question 1

What function is used to concatenate DataFrames in Pandas?

  • pd.merge()

  • pd.concat()

  • pd.append()

  • pd.join()

Question 2

What is the default axis for concatenation in pd.concat()?

  • axis=0 (row-wise)

  • axis=1 (column-wise)

  • It depends on the input DataFrames

  • No default axis is set

Question 3

Which of the following parameters ensures duplicate indices are handled when using pd.concat()?

  • keys

  • ignore_index

  • join

  • sort

Question 4

What will be the result of the following code?

Python
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
result = pd.concat([df1, df2], axis=1)
print(result)


  • A B A B

    0 1 3 5 7

    1 2 4 6 8


  • A B

    0 1 3

    1 2 4

    2 5 7

    3 6 8

  • A B A B

    0 1 2 5 6

    1 3 4 7 8

  • An error

Question 5

What does the keys parameter do in pd.concat()?

  • Merges columns with the same names

  • Assign hierarchical index levels to concatenated DataFrames

  • Appends new keys as columns

  • Sorts the DataFrames before concatenation

Question 6

What will be the result of the following code?

Python
import panads as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})

result = pd.concat([df1, df2], keys=['df1', 'df2'])
print(result)


  • A B

    0 1 3

    1 2 4

    2 5 7

    3 6 8

  • A B

    df1 0 1 3

    1 2 4

    df2 0 5 7

    1 6 8

  • A B

    0 1 3

    1 2 4

    0 5 7

    1 6 8

  • A B

    df1 0 1 3

    1 2 4

    df2 0 5 7

    1 6 8

Question 7

What function is used for a SQL-like merge in Pandas?

  • pd.concat()

  • pd.join()

  • pd.append()

  • pd.merge()

Question 8

What will be the result of the following code?

Python
import pandas as pd
df1 = pd.DataFrame({'ID': [1, 2], 'Name': ['Alice', 'Bob']})
df2 = pd.DataFrame({'ID': [2, 3], 'Score': [90, 80]})

result = pd.merge(df1, df2, on='ID', how='inner')
print(result)


  • ID Name Score

    0 1 Alice NaN

    1 2 Bob 90.0

  • ID Name Score

    0 2 Bob 90.0

  • ID Name Score

    0 2 Bob 90

    1 3 NaN 80

  • An error

Question 9

Which parameter in pd.merge() specifies the type of join to perform?

  • on

  • how

  • sort

  • surfixes

Question 10

What will be the result of the following code?

Python
import pandas as pd 
df1 = pd.DataFrame({'ID': [1, 2], 'Name': ['Alice', 'Bob']})
df2 = pd.DataFrame({'ID': [2, 3], 'Score': [90, 80]})

result = pd.merge(df1, df2, on='ID', how='outer', suffixes=('_df1', '_df2'))
print(result)


  • ID Name Score

    0 1 Alice NaN

    1 2 Bob 90.0

    2 3 NaN 80.0

  • ID Name_df1 Score

    0 1 Alice NaN

    1 2 Bob 90.0

    2 3 NaN 80.0

  • ID Name_df1 Score_df2

    0 1 Alice NaN

    1 2 Bob 90.0

    2 3 NaN 80.0

  • An error

Tags:

There are 10 questions to complete.

Take a part in the ongoing discussion