Reputation: 359
I would like to create dataframe as below. I tried to create it but there some several for loops and many code lines to do. Is there any efficent way to create dataframe?
what I have:
x=array([a,b,c,d])
y=array([A,B,C,D])
id=array([1,2,3,4])
what I want to have:
df:
id type cat
1 a x
2 b x
3 c x
4 d x
1 A y
2 B y
3 C y
4 D y
Upvotes: 0
Views: 1467
Reputation: 602
x = np.array(["a","b","c","d"])
y = np.array(["A","B","C","D"])
id = np.array([1,2,3,4])
df = pd.DataFrame({'id': id, 'x': x, 'y':y})
pd.melt(df, id_vars=['id'], value_vars=['x', 'y'], var_name='cat', value_name='type')
you can use pandas melt function to create your desired output dataframe. Please refer this link pandas.melt for more information on melt
Upvotes: 1
Reputation: 394159
IIUC you can use concat
and pass the 2 dataframes of interest constructed by making a dict with the desired column names and data as the keys, this will vertically stack the dataframes:
In[85]:
x=np.array(['a','b','c','d'])
y=np.array(['A','B','C','D'])
id=np.array([1,2,3,4])
pd.concat([pd.DataFrame({'id':id, 'type':x, 'cat':'x'}, columns=['id','type','cat']), pd.DataFrame({'id':id, 'type':y, 'cat':'y'}, columns=['id','type','cat'])])
Out[85]:
id type cat
0 1 a x
1 2 b x
2 3 c x
3 4 d x
0 1 A y
1 2 B y
2 3 C y
3 4 D y
Upvotes: 0