Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mh0w/657dd27f35a5b6932619636734c1ffa9 to your computer and use it in GitHub Desktop.
Save mh0w/657dd27f35a5b6932619636734c1ffa9 to your computer and use it in GitHub Desktop.
replicate a dataframe n times (extending its length)
import pandas as pd
# Create a toy dataframe for demonstration purposes
my_df = pd.DataFrame({"oa": ["a", "b", "c"]}) # 3 rows
"""
oa
0 a
1 b
2 c
"""
# Choose how many times the original length you want the dataframe to become
n = 3
# Replicate the dataframe such that it is n times its original length
my_df = pd.concat([my_df]*n, ignore_index=True).sort_values("oa") # 9 rows
"""
oa
0 a
3 a
6 a
1 b
4 b
7 b
2 c
5 c
8 c
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment