Search code examples
pythonpandasdataframemultiplicationscalar

How to multiply every row in dataframe by value from csv file


Here's my csv file CSV

I'm trying to take the mean of columns "Angle Min" and "Angle Max" and then multiply every row in the resulting dataframe with the "Angle Conversion Factor" in cell D8. Likewise I want to do the same with "Torque Min" and "Torque Max" (get the mean and then multiply the resulting dataframe by the "Torque Conversion Factor" in Cell E8).

Here's my code so far:

import matplotlib.pyplot as plt                 
from matplotlib.pyplot import show, plot                                            
import numpy as np
from scipy.signal import savgol_filter
import os


def Torque_Analysis(filename):

    #Importing CSV Data and Defining 'AngleDF' and 'TorqueDF'

    Angle_Data = ['Angle Min' , 'Angle Max']                                                                
    Torque_Data = ['Torque Min' , 'Torque Max']
    AngleCol = ['Angle Conversion Factor']
    TorqueCol = ['Torque Conversion Factor']

    AngleConvFactor = pd.read_csv(filename, skiprows=6, usecols = AngleCol, nrows = 1, engine='python')
    TorqueConvFactor = pd.read_csv(filename, skiprows=6, usecols = TorqueCol, nrows = 1, engine='python')

    AngleDF = pd.read_csv(filename, skiprows=17,usecols=Angle_Data, skipfooter=4, engine='python')
    TorqueDF = pd.read_csv(filename, skiprows=17,usecols=Torque_Data, skipfooter=4, engine='python')

    #Calculating the average of 'Angle Min', 'Angle Max'

    col = AngleDF.loc[: ,'Angle Min':'Angle Max']                                                           
    AngleDF['Angle Mean'] = col.mean(axis=1)
    AngleDFmean= AngleDF['Angle Mean']

    #Calculating the average of 'Torque Min', 'Torque Max'

    col = TorqueDF.loc[: ,'Torque Min':'Torque Max']            
    TorqueDF['Torque Mean'] = col.mean(axis=1)
    TorqueDFmean= TorqueDF['Torque Mean']

    #Multiplying Torque Mean values by Torque conversion factor

    TorqueDFmean[:] = TorqueConvFactor * TorqueDFmean

    #Multiplying Angle Mean values by Angle conversion factor

    AngleDFmean[:] = AngleConvFactor * AngleDFmean  

I keep getting the error

"ValueError: cannot set using a slice indexer with a different length than the value".


Solution

  • Your AngleConcFactor and TorqueConvFactor remain as 1x1 DataFrames in your code. Just a slight cleanup of your function might give you what you need:

    def Torque_Analysis(filename):
        Angle_Data = ['Angle Min' , 'Angle Max']                                                                
        Torque_Data = ['Torque Min' , 'Torque Max']
        AngleCol = ['Angle Conversion Factor']
        TorqueCol = ['Torque Conversion Factor']
    
        AngleConvFactor = pd.read_csv(filename, skiprows=6, usecols=AngleCol, nrows=1, engine='python').iat[0,0]
        TorqueConvFactor = pd.read_csv(filename, skiprows=6, usecols=TorqueCol, nrows=1, engine='python').iat[0,0]
    
        AngleDF = pd.read_csv(filename, skiprows=17, usecols=Angle_Data, skipfooter=4, engine='python')
        TorqueDF = pd.read_csv(filename, skiprows=17, usecols=Torque_Data, skipfooter=4, engine='python')
    
        AngleDF["Angle Mean"] = AngleDF[["Angle Min", "Angle Max"]].mean(axis=1)*AngleConvFactor
        TorqueDF["Torque Mean"] = TorqueDF[['Torque Min', "Torque Max"]].mean(axis=1)*TorqueConvFactor
    

    Notice that I've used .iat[0,0] to convert your 1x1 DataFrames to scalar values.