I am working with a pandas dataframe consisting of "recipients" on the y axis and "donors" on the x.
Donor1 Donor2 Donor3 Donor4 Donor5 Donor6
Recipient1 0 4 1 4 7 6
Recipient2 6 0 9 8 5 8
Recipient3 5 3 5 0 9 3
Recipient4 9 2 1 8 0 5
Recipient5 4 5 5 9 4 2
Recipient6 5 4 2 8 5 6
The maxValueIndex code
maxValueIndex = df_test_bid.idxmax(axis = 1)
print(str(maxValueIndex))
produces: (index of max value in that row)
Recipient1 Donor5
Recipient2 Donor3
Recipient3 Donor5
Recipient4 Donor1
Recipient5 Donor4
Recipient6 Donor4
dtype: object
How can I convert each recipient/donor pair into a single string? In other words, how can I iterate through each recipient/donor pair?
Trying to iterate with a for loop only gets me the donors.
CodePudding user response:
if you want to convert 2 columns to one:
dataframename.apply(lambda x: str(x["<first_col_name>"]) " " str(x["<second_col_name>"]), axis = 1)
CodePudding user response:
The issue is that idxmax
creates a pd.Series
with Recipients
as the index. To loop through them as expected, try:
df2 = df.idxmax(axis=1)
for i, x in df2.iteritems():
print(i ' ' x)
Output:
Recipient1 Donor5
Recipient2 Donor3
Recipient3 Donor5
Recipient4 Donor1
Recipient5 Donor4
Recipient6 Donor4
To use the more panda's friendly way, convert the output to a dataframe and reset the index:
df2 = df.idxmax(axis=1)
df2 = pd.DataFrame(df2).reset_index()
df2 = df2.apply(lambda x: x['index'] ' ' x[0], axis=1)
print(df2)
Output:
0 Recipient1 Donor5
1 Recipient2 Donor3
2 Recipient3 Donor5
3 Recipient4 Donor1
4 Recipient5 Donor4
5 Recipient6 Donor4