hugmath
hugmath

Reputation: 21

How to rename row's in Python?

I want to rename some row in my output DataFrame but I don't know how.

Here you can see my code (main.py):

import os
os.getcwd()
import pandas as pd
import loaddata as ld
import symmetryvalues as sv
import numpy as np

#%% Laden der Daten für Symmetriewerte
dataListStep = ld.loadData("../data/mpi/onlycsv/StepData")
indexStepData = 1
stepData = dataListStep[indexStepData]
resultsPerRowRatio = list()

leftMean = np.asarray([stepData.iloc[:, i].mean() for i in range(0, 5)])
rightMean = np.asarray([stepData.iloc[:, i].mean() for i in range(5, 10)])

#%% Calculation of the Mean Values of the columns 
dataFrameStepLeftMean = pd.DataFrame({'leftMean': leftMean})
dataFrameStepRightMean = pd.DataFrame({'rightMean': rightMean})

symRatio = sv.symmetryRatio(dataFrameStepLeftMean.leftMean, dataFrameStepRightMean.rightMean)
print("Das Ergebnis Symmetry Ratio über die Mittelwerte  lautet: " +str(symRatio))

#%% Line Scan
for i in range(len(stepData)):
    stepDataLeft = stepData.to_numpy()[i, 0:5]
    stepDataRight = stepData.to_numpy()[i, 5:10]
    dataFrameOfStepDataLeft = pd.DataFrame({'stepDataLeft': stepDataLeft})
    dataFrameOfStepDataRight = pd.DataFrame({'stepDataRight': stepDataRight})
    resultsPerRowRatio.append(sv.symmetryRatio(dataFrameOfStepDataLeft.stepDataLeft, dataFrameOfStepDataRight.stepDataRight))
print("Das Ergebnis der Einzelnen Schritte über Symmetry Ratio ist:" +str(resultsPerRowRatio))

and the other one (symmetryvalues.py):

def symmetryRatio(L, R):
    result = L/R
    return result

This is my Output:

The result of Symmetry Ratio over the mean values is: 0    0.833333
1    0.888889
2    1.294118
3    1.000000
4    1.445026
dtype: float64
The result of the individual steps over Symmetry Ratio is:[0    0.833333
1    0.888889
2    1.333333
3    1.000000
4    1.470588
dtype: float64, 0    0.846154
1    0.894737
2    1.250000
3    1.000000
4    1.404762
dtype: float64, 0    0.818182
1    0.900000
2    1.285714
3    1.000000
4    1.428571
dtype: float64, 0    0.840000
1    0.875000
2    1.333333
3    1.000000
4    1.500000
dtype: float64, 0    0.826087
1    0.882353
2    1.285714
3    1.000000
4    1.428571
dtype: float64]

This is my test csv data:

""""

1.00,0.80,0.40,0.20,0.50,1.20,0.90,0.30,0.20,0.34

1.10,0.85,0.50,0.21,0.59,1.30,0.95,0.40,0.21,0.42

0.90,0.90,0.45,0.23,0.50,1.10,1.00,0.35,0.23,0.35

1.05,0.70,0.40,0.28,0.57,1.25,0.80,0.30,0.28,0.38

0.95,0.75,0.45,0.30,0.60,1.15,0.85,0.35,0.30,0.42

""""

But now I want to have an output like this:

    The result of Symmetry Ratio over the mean values is: 
    Stride Length Ratio    0.833333
    Stand Duration Ratio    0.888889
    Swing Duration Ratio    1.294118
    Douple Support Time Ratio   1.000000
    Relation Swing Stand Ratio    1.445026
    dtype: float64

    The result of the individual steps over Symmetry Ratio is:
    [ First Step:
    Stride Length Ratio    0.833333
    Stand Duration Ratio   0.888889
    Swing Duration Ratio     1.333333
    Douple Support Time Ratio    1.000000
    Relation Swing Stand Ratio   1.470588
    dtype: float64, 

    Second Step:
    Stride Length Ratio   0.846154
    Stand Duration Ratio   0.894737
    Swing Duration Ratio    1.250000
    Douple Support Time Ratio   1.000000
    Relation Swing Stand Ratio   1.404762
    dtype: float64, 

    Third Step:
    Stride Length Ratio    0.818182
    Stand Duration Ratio   0.900000
    Swing Duration Ratio    1.285714
    Douple Support Time Ratio   1.000000
    Relation Swing Stand Ratio   1.428571
    dtype: float64, 

    Fourth step:
    Stride Length Ratio   0.840000
    Stand Duration Ratio   0.875000
    Swing Duration Ratio    1.333333
    Douple Support Time Ratio   1.000000
    Relation Swing Stand Ratio    1.500000
    dtype: float64, 

    Fifth step:
    Stride Length Ratio   0.826087
    Stand Duration Ratio   0.882353
    Swing Duration Ratio   1.285714
    Douple Support Time Ratio   1.000000
    Relation Swing Stand Ratio   1.428571
    dtype: float64]

How can I reach this? Thanks for helping me.

Upvotes: 2

Views: 681

Answers (1)

mlg
mlg

Reputation: 38

You can use Dataframe.rename() like this, supposing df is the dataframe you want to change the indexes by strings:

df.rename(index={0: "Stride Length Ratio", 1:"Stand Duration Ratio"})

Upvotes: 1

Related Questions