python - 如何在 Python 中重命名行?
问题描述
我想重命名输出 DataFrame 中的某些行,但我不知道如何。
在这里你可以看到我的代码(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))
另一个(symmetryvalues.py):
def symmetryRatio(L, R):
result = L/R
return result
这是我的输出:
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]
这是我的测试 csv 数据:
""""
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
""""
但现在我想要这样的输出:
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]
我怎样才能做到这一点?谢谢你帮助我。
解决方案
您可以像这样使用 Dataframe.rename(),假设 df 是您要通过字符串更改索引的数据框:
df.rename(index={0: "Stride Length Ratio", 1:"Stand Duration Ratio"})
推荐阅读
- r - 不同的结果应用 geom_tile 而不是 heatmap
- php - SQLSTATE [23000]:完整性约束违规:1052 使用 laravel 连接
- bash - 使用 sed 检查和替换字母数字模式
- excel - Excel - 从基于非唯一条件的范围内返回多个唯一查询,即相似排名
- python - NLP ML 如何知道文本分类器中使用的词的权重?
- c# - 内联初始化多个实例#
- php - PHP中的目录列表
- angular - *ngIf 为 false 时组件不会被销毁
- reactjs - 渲染反应状态时将img src包装在“”中
- sql-server - 使用序列根据条件增加计数器