0

I am using Python 2.7 and Pandas 3.3.4. I have 2 DataFrames: df1 and df2. As in code below, I want to determine the max value column for each row in df1, copy that column value in df2 to a MaxValueCol in df2. Below code (of many variations) works for all but the last line that copies values in df2. The error I get is: "ValueError: Wrong number of items passed 100, placement implies 1"

If I replace the with last line with the following commented statement it runs, but is a multi-column df, instead of the desired single column of max values.

Thank you.

enter code here
import pandas as pd
import numpy as np

np.random.seed(0)
df1 = pd.DataFrame(np.random.randn(100,6),columns=list('ABCDEF'))
df2 = pd.DataFrame(np.random.randn(100,6),columns=list('ABCDEF'))
df1['maxColumn'] = df1.idxmax(axis=1)
df2['maxValue'] = df2[pd1.loc[:,'maxColumn']]
# maxValueV = df2[df1.loc[:,'maxColumn']]

Below is modified code to shorted number of rows and columns and below that show expected results. The need is to find the max ranked column of every row in df1, then move that column in every row of df2 to maxRankVal2 column in df2.

import pandas as pd
import numpy as np

np.random.seed(4)
df1 = pd.DataFrame(np.random.randn(3,3),columns=list('ABC'))
df2 = pd.DataFrame(np.random.randn(3,3),columns=list('ABC'))
df2['maxRankCol1'] = pd1.idxmax(axis=1)
df2['maxRankVal1'] = pd1.max(axis=1)
temp = pd2[pd2['maxRankCol1']]
#df2['maxRankVal2'] = pd2[pd2['maxRankCol1']] # This statement does not work
df2.insert(3,'maxRankVal2',0) # add Expected Results Column
print(df2.head(4))

df2 Expected Results
      A       B       C   maxRankVal2 maxRankCol1 maxRankVal1
0 -1.1474  0.6186 -0.0879   0.6186        B        0.499951
1  0.4250  0.3322 -1.1568   0.4250        A        0.693599
2  0.3509 -0.6068  1.5469  -0.6068        B        0.598575
Jim Gunn
  • 21
  • 7

1 Answers1

0
import pandas as pd
import numpy as np

np.random.seed(0)
pd1 = pd.DataFrame(np.random.randn(100,6),columns=list('ABCDEF'))
pd2 = pd.DataFrame(np.random.randn(100,6),columns=list('ABCDEF'))

pd2['maxColumn'] = pd1.max().sort_values()[-1:].index.values[0]
pd2['maxValue'] = pd1.max().sort_values()[-1:][0]

print(pd2[['maxColumn', 'maxValue']].head(1))
  maxColumn  maxValue
0         C  2.696224
Rich Andrews
  • 1,590
  • 8
  • 12
  • Thank you for your input. But this does not do what i want. I want to find the df1maxrank column (and df1maxValue for reference) by row (axis=1) from pd1. Then move that column value by row in df2 to another df2maxValue column. I am learning. So again many thanks. – Jim Gunn Apr 04 '19 at 22:32
  • Please update your question with that clarification, and consider placing the desired output results in it too. If you haven't, give this a read. I'll take another shot at the answer. https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Rich Andrews Apr 05 '19 at 13:47
  • Thank you for offer to help. I added info you requested as answer. Please see above. – Jim Gunn Apr 05 '19 at 16:52