gpt-4.1-2025-04-14· OpenAI
LMArena Human Preference 140K
divide df by row mean
- User
divide df by row mean
- Assistant
Certainly! If you have a pandas DataFrame
dfand you want to divide each element in a row by the mean of that row, you can do this easily using pandas' broadcasting. Here’s how you can do it:import pandas as pd # Example DataFrame df = pd.DataFrame({ 'A': [2, 4, 6], 'B': [4, 8, 12], 'C': [6, 12, 18] }) # Divide each row by its mean df_divided = df.div(df.mean(axis=1), axis=0) print(df_divided)Explanation:
df.mean(axis=1)computes the mean of each row, resulting in a Series of row means.df.div(..., axis=0)divides each row by the corresponding value in the Series (broadcasting along rows).
Result:
A B C 0 0.5 1.0 1.5 1 0.5 1.0 1.5 2 0.5 1.0 1.5Let me know if you need this for columns or have a specific DataFrame!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.