Jun-03-2024, 07:54 PM
To find the smallest and largest values in each row of a pandas DataFrame, you can use the min() and max() functions along the appropriate axis. Here's how:
python
Copy code
import pandas as pd
# Create a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
# Find the smallest value in each row
smallest_values = df.min(axis=1)
# Find the largest value in each row
largest_values = df.max(axis=1)
print("Smallest values in each row:")
print(smallest_values)
print("\nLargest values in each row:")
print(largest_values)
This will output the smallest and largest values for each row in the DataFrame.
python
Copy code
import pandas as pd
# Create a sample DataFrame
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
df = pd.DataFrame(data)
# Find the smallest value in each row
smallest_values = df.min(axis=1)
# Find the largest value in each row
largest_values = df.max(axis=1)
print("Smallest values in each row:")
print(smallest_values)
print("\nLargest values in each row:")
print(largest_values)
This will output the smallest and largest values for each row in the DataFrame.