Python Filling dataframe values based on Column Index present in another dataframe value

Anku

I have 2 dataframes like below: dataframe df1:

id  val1    val2    val3    val4    val5
abc 0.0 1.0 4.0 3.0 4.0
dsssd   0.0 1.0 1.0 1.0 1.0
dsd 0.0 4.0 7.0 

Another dataframe df2:

id  val1    val2    val3    val4    val5
abc 88 76 55 43 21
dsssd   92.4 21.3 22 45 49
dsd 22.3 87.2 78.2

df1 contains column index as values. I want to create df3 which has corresponding index value from df2. Expected results df3:

id  val1    val2    val3    val4    val5
abc 88  76  21  43  21
dsssd   92.4    21.3    21.3    21.3    21.3
dsd 22.3    nan nan 

I have explored df.lookup and iloc, but couldn't get how it can be done. I am still looking to find solution. Meanwhile I posted it here, if anyone knew how it's done.

import pandas as pd
import numpy as np

df1= pd.DataFrame({'id': ['abs', 'dssd', 'dsd'],
                   'val1': [0.0, 0.0, 0.0],
                   'val2': [1.0, 1.0, 4.0],
                   'val3': [4.0, 1.0, 7.0],
                   'val4': [3.0, 1.0, np.nan],
                   'val5': [4.0, 1.0, np.nan]})


df2= pd.DataFrame({'id': ['abs', 'dssd', 'dsd'],
                   'val1': [88.0, 92.4, 22.3],
                   'val2': [76.0, 21.3, 87.2],
                   'val3': [55.0, 22.0, 78.2],
                   'val4': [43.0, 45.0, np.nan],
                   'val5': [21.0, 49.0, np.nan]})

Thanks!

jezrael

You can use DataFrame.set_index with DataFrame.stack for reshape, add counter column by GroupBy.cumcount, left join by DataFrame.merge and last pivoting by DataFrame.pivot with change order of id by DataFrame.reindex:

df11 = df1.set_index('id').stack().rename_axis(index=['id','v']).reset_index(name='idx')
# print (df11)

df22 = df2.set_index('id').stack().rename_axis(index=['id','v']).reset_index(name='val')
df22['idx'] = df22.groupby('id').cumcount()
# print (df22)


df = (df11.merge(df22, on=['id','idx'], how='left')
          .pivot(index='id', columns='v_x', values='val')
          .reindex(df1['id'])
          .rename_axis(None, axis=1)
          .reset_index()
          )
print (df)
     id  val1  val2  val3  val4  val5
0   abs  88.0  76.0  21.0  43.0  21.0
1  dssd  92.4  21.3  21.3  21.3  21.3
2   dsd  22.3   NaN   NaN   NaN   NaN

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Filling dataframe column with values of another dataframe

From Dev

Filling NaN in DataFrame based on the values of a column

From Dev

Filling a column in a dataframe based on a column in another dataframe in r

From Dev

How to filter a dataframe based on the values present in the list in the rows of a column in Python?

From Dev

Optimal filling of pandas DataFrame column by matching values in another DataFrame

From Dev

filling in a column in pandas dataframe based on multiple conditions in another column

From Dev

Python Dataframe:Change values of a column based on another column?

From Dev

Python match a column name based on a column value in another dataframe

From Dev

Filling NaNs with values from column of another dataframe by comparing the columns

From Dev

Replace column values based on another dataframe python pandas - better way?

From Dev

Add column from one dataframe to another, for values present in overlapping column

From Dev

How to get value from python dataframe based on column index?

From Dev

Create a column based on another dataframe values

From Dev

Python dataframe check if a value in a column dataframe is within a range of values reported in another dataframe

From Dev

updating column in one dataframe with value from another dataframe based on matching values

From Dev

How get sum of values in another dataframe based column value in first dataframe?

From Dev

Conditional Sums based on another DataFrame column value

From Dev

change dataframe values based on next column value

From Dev

Replacing multiple values from a column in a dataframe based on another dataframe

From Dev

Filling a column with NA, based on the value in an another column

From Dev

Filling a dataframe column based on multiple conditional choices

From Dev

Filling a dataframe column based on multiple conditional choices

From Dev

Find Pandas dataframe column based on values, in Python

From Dev

Find Pandas dataframe column based on values, in Python

From Dev

python & pandas - Drop rows where column values are index values in another DataFrame

From Dev

Index into dataframe by column value

From Dev

Python Pandas: Find index based on value in DataFrame

From Dev

Replace values of a column based on another column having as input a dataframe

From Dev

Select column dynamically in Pandas dataframe based on values in a list or another column

Related Related

  1. 1

    Filling dataframe column with values of another dataframe

  2. 2

    Filling NaN in DataFrame based on the values of a column

  3. 3

    Filling a column in a dataframe based on a column in another dataframe in r

  4. 4

    How to filter a dataframe based on the values present in the list in the rows of a column in Python?

  5. 5

    Optimal filling of pandas DataFrame column by matching values in another DataFrame

  6. 6

    filling in a column in pandas dataframe based on multiple conditions in another column

  7. 7

    Python Dataframe:Change values of a column based on another column?

  8. 8

    Python match a column name based on a column value in another dataframe

  9. 9

    Filling NaNs with values from column of another dataframe by comparing the columns

  10. 10

    Replace column values based on another dataframe python pandas - better way?

  11. 11

    Add column from one dataframe to another, for values present in overlapping column

  12. 12

    How to get value from python dataframe based on column index?

  13. 13

    Create a column based on another dataframe values

  14. 14

    Python dataframe check if a value in a column dataframe is within a range of values reported in another dataframe

  15. 15

    updating column in one dataframe with value from another dataframe based on matching values

  16. 16

    How get sum of values in another dataframe based column value in first dataframe?

  17. 17

    Conditional Sums based on another DataFrame column value

  18. 18

    change dataframe values based on next column value

  19. 19

    Replacing multiple values from a column in a dataframe based on another dataframe

  20. 20

    Filling a column with NA, based on the value in an another column

  21. 21

    Filling a dataframe column based on multiple conditional choices

  22. 22

    Filling a dataframe column based on multiple conditional choices

  23. 23

    Find Pandas dataframe column based on values, in Python

  24. 24

    Find Pandas dataframe column based on values, in Python

  25. 25

    python & pandas - Drop rows where column values are index values in another DataFrame

  26. 26

    Index into dataframe by column value

  27. 27

    Python Pandas: Find index based on value in DataFrame

  28. 28

    Replace values of a column based on another column having as input a dataframe

  29. 29

    Select column dynamically in Pandas dataframe based on values in a list or another column

HotTag

Archive