Add lists, dictionaries, Series and DataFrame rows with concat(). Filter incoming records, manage indexes and add columns.
Show Table of Contents ↓import pandas as pd
df = pd.DataFrame({"NAME": ["Ravi", "Raju", "Alex"], "ID": [1, 2, 3],
"MATH": [30, 40, 50], "ENGLISH": [20, 30, 40]})
print(df)
assert df.shape == (3, 4)Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40my_list = ["New 1", 7, 66, 56]
new_row = pd.DataFrame([my_list], columns=df.columns)
added = pd.concat([df, new_row])
print(added)
assert added.index.tolist() == [0, 1, 2, 0]
assert len(df) == 3Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
0 New 1 7 66 56rows = [["New 1", 7, 66, 56], ["New 2", 8, 63, 59], ["New 3", 9, 69, 69]]
incoming = pd.DataFrame(rows, columns=df.columns)
combined = pd.concat([df, incoming], ignore_index=True)
print(combined)
assert combined.index.tolist() == list(range(6))Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 New 1 7 66 56
4 New 2 8 63 59
5 New 3 9 69 69record = {"NAME": "New 5", "ID": 10, "MATH": 78, "ENGLISH": 80}
from_dict = pd.concat([df, pd.DataFrame([record])], ignore_index=True)
print(from_dict)
assert from_dict.iloc[-1]["NAME"] == "New 5"Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 New 5 10 78 80record_series = pd.Series({"NAME": "New 5", "ID": 10, "MATH": 78, "ENGLISH": 80})
series_row = record_series.to_frame().T.astype(df.dtypes.to_dict())
from_series = pd.concat([df, series_row], ignore_index=True)
print(from_series)
pd.testing.assert_frame_equal(from_series, from_dict)Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 New 5 10 78 80working = df.reset_index(drop=True).copy()
working.loc[len(working)] = ["new 6", 10, 80, 88]
print(working)
assert len(working) == 4 and len(df) == 3Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 new 6 10 80 88candidates = [["new 4", 4, 80, 88], ["new 5", 5, 81, 84], ["new 6", 6, 76, 78]]
accepted_rows = []
for row in candidates:
if row[2] + row[3] >= 160:
accepted_rows.append(row)
accepted = pd.DataFrame(accepted_rows, columns=df.columns)
loop_result = pd.concat([df, accepted], ignore_index=True)
print(loop_result)
assert loop_result["NAME"].tolist()[-2:] == ["new 4", "new 5"]Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 new 4 4 80 88
4 new 5 5 81 84candidate_df = pd.DataFrame(candidates, columns=df.columns)
qualifies = candidate_df[["MATH", "ENGLISH"]].sum(axis=1) >= 160
filtered = pd.concat([df, candidate_df.loc[qualifies]], ignore_index=True)
inverse = pd.concat([df, candidate_df.loc[~qualifies]], ignore_index=True)
print(filtered)
print("Inverse selection:")
print(inverse)
pd.testing.assert_frame_equal(filtered, loop_result)
assert inverse.iloc[-1]["NAME"] == "new 6"Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 new 4 4 80 88
4 new 5 5 81 84
Inverse selection:
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 new 6 6 76 78with_science = df.copy()
with_science["SCIENCE"] = [34, 35, 36]
print(with_science)
assert "SCIENCE" not in df.columnsExpected output
NAME ID MATH ENGLISH SCIENCE
0 Ravi 1 30 20 34
1 Raju 2 40 30 35
2 Alex 3 50 40 36partial = pd.DataFrame([{"NAME": "New 7", "ID": 11}])
outer = pd.concat([df, partial], ignore_index=True, sort=False)
inner = pd.concat([df, partial], ignore_index=True, join="inner")
sorted_columns = pd.concat([df, partial], ignore_index=True, sort=True)
print(outer)
print("Inner columns:", inner.columns.tolist())
print("Sorted columns:", sorted_columns.columns.tolist())
assert pd.isna(outer.iloc[-1]["MATH"])
assert inner.columns.tolist() == ["NAME", "ID"]Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30.0 20.0
1 Raju 2 40.0 30.0
2 Alex 3 50.0 40.0
3 New 7 11 NaN NaN
Inner columns: ['NAME', 'ID']
Sorted columns: ['ENGLISH', 'ID', 'MATH', 'NAME']try:
pd.concat([df, new_row], verify_integrity=True)
except ValueError:
print("Duplicate index labels rejected")
else:
raise AssertionError("Expected an overlapping index error")
repeat = pd.concat([df, df.iloc[[0]]], ignore_index=True, verify_integrity=True)
print("Repeated IDs:", repeat.loc[repeat["ID"].duplicated(), "ID"].tolist())
assert repeat["ID"].duplicated().sum() == 1Expected output
Duplicate index labels rejected
Repeated IDs: [1]batch = pd.DataFrame([["Duplicate Ravi", 1, 90, 90], ["new 8", 8, 85, 85],
["new 9", 9, 60, 70]], columns=df.columns)
valid = batch[["MATH", "ENGLISH"]].sum(axis=1).ge(160) & ~batch["ID"].isin(df["ID"])
review = batch.loc[~valid].copy()
result = pd.concat([df, batch.loc[valid]], ignore_index=True)
print(result)
print("Review IDs:", review["ID"].tolist())
assert result["ID"].is_unique and len(result) == 4
assert len(review) == 2Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 new 8 8 85 85
Review IDs: [1, 9]records = [{"NAME": "New 10", "ID": 10, "MATH": 70, "ENGLISH": 80},
{"NAME": "New 11", "ID": 11, "MATH": 60, "ENGLISH": 90}]
answer = pd.concat([df, pd.DataFrame(records)], ignore_index=True)
print(answer)
assert len(answer) == 5 and answer["ID"].is_unique
assert len(df) == 3 and answer.index.tolist() == list(range(5))Expected output
NAME ID MATH ENGLISH
0 Ravi 1 30 20
1 Raju 2 40 30
2 Alex 3 50 40
3 New 10 10 70 80
4 New 11 11 60 90Open in Google Colab View on GitHub
Run examples in order, inspect added rows and try the exercise. Save a copy to keep edits.
Reference: Pandas concat() documentation.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.