我将以下数据集存储在CSV文件中:
Name Salary
John 50000
Eric 150000
Paul 100000
我想获得中位数工资并在SAME csv文件中输出此值,如下所示:
Name Salary Median
John 50000 100000
Eric 150000 100000
Paul 100000 100000
代码:
import numpy as np
from numpy import genfromtxt
dataset = genfromtxt('C:\\Users\abc.csv',delimiter=',')
x=dataset[2:,0]
y=np.median(x)
答案 0 :(得分:1)
现在您已经x
和y
了,您可以使用np.savetxt
转储它们。
np.savetxt('C:\\Users\abc.csv', np.hstack((x[2:], y.reshape(-1, 1))) , delimiter=',')
答案 1 :(得分:0)
尝试大熊猫,在你的情况下:
>>>files = 'example.csv'
>>>df = pd.read_csv(files)
>>>df = df.convert_objects(convert_numeric=True)
>>>df['Median'] = df['Salary'].median()
>>>df.to_csv(files)
更多解释
>>>files = 'example.csv'
>>>df = pd.read_csv(files)
>>>df = df.convert_objects(convert_numeric=True)
>>>df
A B C D
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
df['new']=13
>>>df
A B C D new
0 1 2 3 4 13
1 5 6 7 8 13
2 9 10 11 12 13
>>>df['new'] = df['new'] +1
>>>df
A B C D new
0 1 2 3 4 14
1 5 6 7 8 14
2 9 10 11 12 14
>>>df.insert(2,'win',22)
>>>df
A B win C D new
0 1 2 22 3 4 14
1 5 6 22 7 8 14
2 9 10 22 11 12 14