I have a dataframe such as:
from pandas import DataFrame
import pandas as pd
x = DataFrame.from_dict({'farm' : ['A','B','A','B'],
'fruit':['apple','apple','pear','pear']})
How can I copy it N
times with an id, eg. to output (for N=2
):
farm fruit sim
0 A apple 0
1 B apple 0
2 A pear 0
3 B pear 0
0 A apple 1
1 B apple 1
2 A pear 1
3 B pear 1
I tried an approach which works on dataframes in R:
from numpy import arange
N = 2
sim_ids = DataFrame(arange(N))
pd.merge(left=x, right=sim_ids, how='left')
but this fails with the error MergeError: No common columns to perform merge on
.
Thanks.