0

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
1
  • 1
    IIUC you should be able to do pd.concat([pd.DataFrame({'id:id, 'type':x, 'cat':'x'}), pd.DataFrame({'id:id, 'type':y, 'cat':'y'})]) Commented Jun 15, 2017 at 11:03

2 Answers 2

1
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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.