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