python - Adding the result of an array in a csv file -
i tried make algorithm: random draw between 0 , 1(tir).si tir '<'pred xestime2= 1 else xestime2=0. wish apply algorithm in df ['x3'] had 0 in values of x3 columns. explains thats have error in code. coding:
df = pd.read_csv(fname3, header=none) print df[:15] df['x2'] = df['x1'].round() print df[:15] s = stringio() df.to_csv("c:/users/lenovo/desktop/nouveau dossier (2)/resultats2.csv", header=none, index=false) #print(s.getvalue()) ##########################################"""""""" row in df['x1']: x = np.random.randint(0,2,10) row1 in x: if row1 < row: df['x3']=0 else: df['x3']=1 #print df[:15] df.to_csv("c:/users/lenovo/desktop/nouveau dossier (2)/resultats2.csv", header=none, index=false)
since you've tagged pandas, i'll use read_csv
:
in [1]: df = pd.read_csv('foo.csv', header=none) in [2]: df out[2]: 0 1 0 0 0.487130 1 0 0.248932 2 0 0.248932 3 1 0.405285 4 1 0.405285 5 1 0.405285 6 1 0.405285
then can round column (to nearest 1):
in [3]: df[2] = df[1].round() in [4]: df out[4]: 0 1 2 0 0 0.487130 0 1 0 0.248932 0 2 0 0.248932 0 3 1 0.405285 0 4 1 0.405285 0 5 1 0.405285 0 6 1 0.405285 0
if values on half, they'd rounded 1.
since asked sending stringio, it's same file:
in [11]: s = stringio() in [12]: df.to_csv(s, header=none, index=false) # alternatively write file df.to_csv('foo.csv', header=none, index=false) in [13]: print(s.getvalue()) 0.0,0.4871303471776849,0.0 0.0,0.2489319061991417,0.0 0.0,0.2489319061991417,0.0 1.0,0.4052854182229446,0.0 1.0,0.4052854182229446,0.0 1.0,0.4052854182229446,0.0 1.0,0.4052854182229446,0.0
Comments
Post a Comment