Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

divide df by row mean

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with o3-mini’s answer.
  1. User

    divide df by row mean

  2. Assistant

    Certainly! If you have a pandas DataFrame df and 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.5
    

    Let me know if you need this for columns or have a specific DataFrame!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.

Identical first prompt