Last active
January 30, 2024 09:26
-
-
Save mh0w/657dd27f35a5b6932619636734c1ffa9 to your computer and use it in GitHub Desktop.
replicate a dataframe n times (extending its length)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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