Wie kann ich den Maximalwert über mehrere Spalten in Pandas berechnen?

Susan Sarandon
Freigeben: 2024-10-17 20:53:30
Original
697 Leute haben es durchsucht

How Can I Calculate the Maximum Value Across Multiple Columns in Pandas?

Find the Maximum Value Across Multiple Columns in Pandas

Suppose you have a dataframe with several columns and wish to create a new column containing the maximum value from two or more existing columns. For instance, given columns A and B, you need to create a column C where:

C = max(A, B)
Nach dem Login kopieren

To accomplish this task:

  1. Use the max function along with axis=1 to calculate the maximum value for each row across the specified columns:
df[["A", "B"]].max(axis=1)
Nach dem Login kopieren
  1. Assign the result to a new column:
df["C"] = df[["A", "B"]].max(axis=1)
Nach dem Login kopieren

This generates a new column C containing the maximum value for each row between columns A and B:

A B C
1 -2 1
2 8 8
3 1 3

Note that this technique can be generalized to find the maximum value across any number of columns.

Das obige ist der detaillierte Inhalt vonWie kann ich den Maximalwert über mehrere Spalten in Pandas berechnen?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!