Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating new dataframe with column name and value from other dataframe

I have df1 like this:

        id
   0   MC01
   1   MC02
   2   MC03

Then another df2 like this:

   employee   mins
      A        8.0
      B        7.5
      C        6.3
      D        9.1
      E        8.4
      F        6.1

I want to combine both dataframe into matrix like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   employee   MC01    MC02    MC03
      A        8.0     8.0     8.0
      B        7.5     7.5     7.5
      C        6.3     6.3     6.3
      D        9.1     9.1     9.1
      E        8.4     8.4     8.4
      F        6.1     6.1     6.1

And this code will running continuously with different value/datashape.
Please help me on this matter.
Thanks in advance!

>Solution :

You can do something like this:

df3 = pd.DataFrame()
df3['employee'] = df2['employee']
for col in df1['id']:
    df3[col] = df2['mins']
>>> df3
    employee    MC01    MC02    MC03
0   A   8.0 8.0 8.0
1   B   7.5 7.5 7.5
2   C   6.3 6.3 6.3
3   D   9.1 9.1 9.1
4   E   8.4 8.4 8.4
5   F   6.1 6.1 6.1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading