Home > Net >  Creating new dataframe with column name and value from other dataframe
Creating new dataframe with column name and value from other dataframe

Time:11-30

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:

   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!

CodePudding user response:

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
  • Related