【发布时间】:2019-03-01 03:39:32
【问题描述】:
我正在尝试制作一个程序,如果在其中识别出某个元素,它将乘以 Dataframe 行。例如,假设我有一个数据框:
A B C D E F G
1 0 -1 2 -4 C 5
4 1 5 7 -0.2 E 7
每当 F 列包含一个字母时,除了最后一列外,它应该使用以下数字乘以该行: C= 2.8 E=1.4
所以最终的输出会是这样的:
A B C D E F G
2.8 0 -2.8 5.6 -11.2 C 5
5.6 1.4 7 9.8 -0.28 E 7
这是我尝试使用的代码:
import pandas as pd
import csv
data= pd.read_csv("file.txt", sep= '\t')
U= data.drop('xyz', axis= 1)
for col in U:
U=col * 2.63
for Z in U:
Z= pd.DataFrame(U)
with open('File.tbl', 'r') as f:
P=list(f)
del P[0]
B=[]
O=[]
for o in P:
J=o.split()
B.append(J[:4])
T=(J[3:4])
O.append(J[2:3])
column=['A','B','C','D']
Y= pd.DataFrame(B, columns= column)
D= Y.drop(0)
D=D.reset_index(drop=True)
M = pd.concat([Z, D], sort=False, axis= 1) #Concatenating both the dataframes
S= pd.DataFrame(M)
x=O
while True:
x= C = 2.8
x= E = 1.4
col_Number = col + '_Number'
Z[col_Number] = (Z[col]*(x) - Z.max()) / Z.max() - Z.min() #multiply the Z-score rows
在运行这个程序时,它显示 None 并且只显示最后一列,即。 E. 上述公式从每一列中选择最大值和最小值并进行计算。 Z[col] 是行值,即。 1、0、-1等要相乘。
我尝试过使用 loc 方法,但没有帮助。任何帮助将不胜感激。
【问题讨论】:
标签: python python-3.x dataframe formula matrix-multiplication