如何计算csv文件中数据的均值,中位数,最小值,最大值和众数并返回2d数组中的值? (python)
Data = Employee_name,Date,Salary,Age
Ramesh,04/04/19,20000,25
Mahesh,04/04/19,10000,30
John,04/04/19,15000,15
Watson,05/04/19,19000,20
这是我的代码: 数据= pd.read_csv('Data_file.csv')
对于数据行:
print line
try:
mean= (round(getattr(data,line).mean(),2))
except:
mean= ("__")
try:
min= (round(getattr(data,line).min(),2))
except:
min= ("__")
try:
max= (round(getattr(data,line).max(),2))
except:
max= ("__")
try:
range=(round(getattr(data,line).max()-(getattr(data,line).min()),2))
except:
range= ("__")
try:
std= (round(getattr(data,line).std(),2))
except:
std= ("__")
try:
mode=round(getattr(data,line).mode()[0],2)
except:
mode= ("__")
try:
kurt=round(getattr(data,line).kurtosis(),2)
except:
kurt= ("__")
try:
skew= (round(getattr(data,line).skew(),2))
except:
skew= ("__")
try:
med= (round(getattr(data,line).median(),2))
except:
med= ("__")
try:
var= (round(getattr(data,line).var(),2))
except:
var= ("__")
Mean=[ ]
Min= [ ]
Max= [ ]
Range= [ ]
Standard_deviation=[ ]
Mode=[ ]
Kurtosis=[ ]
Skewness=[ ]
Median=[ ]
Sample_Variance=[ ]
Mean.append(mean)
Min.append(min)
Max.append(max)
Range.append(range)
Standard_deviation.append(std)
Mode.append(mode)
Kurtosis.append(kurt)
Skewness.append(skew)
Median.append(med)
Sample_Variance.append(var)
print(Mean,Min,Max,Range,Standard_deviation,Mode,Kurtosis,Skewness,Median,Sample_Variance)
在这里使用try,除了我处理了employeee_name和date的异常。 对于进一步,我不知道。我是python新手