The code below is meant to iterate [Val1, Val2, Val3, Val4] into a csv file. It saves each iteration to the csv code with the dataframe.to_csv("sales.csv", index=False, mode='a', header=False) code. However the code makes a separate row for each Val value as visualized in the Ouput. I want to make it so that val1-4 are printed row by row for each iteration. How could I do that so I could get the Expected output. as the resultant.
from numpy import random
import pandas
Values = random.randint(100, size=(100000))
Number_array = random.randint(100, size=(1000))
for n in range(len(Values)):
val1 = np.sum(Number_array) + Values[n] * len(Number_array)
val2 = np.sum([Number_array])
val3 = val1 * val2
val4 = n * 2
data =[Val1, Val2, Val3, Val4]
dataframe = pandas.DataFrame(data)
dataframe.to_csv("input.csv", index=False, mode='a', header=False)
input.csv file:
Val1, Val2, Val3, Val4
Output:
Val1, Val2, Val3, Val4
49793
48793
-1865417447
0
82793
48793
-255248447
2
Expected output
Val1, Val2, Val3, Val4
49793,48793, -1865417447, 0
82793, 48793, -255248447, 2
dataframe = pandas.DataFrame([data])instead ofdataframe = pandas.DataFrame(data)? Just guessing looking at your code, haven't tried. Looks like you want to create 4 columns but you pass a single list instead of list of the list which is why you end up with 1 column instead of 4.file-objectappend which take much time then append to list and then at to file in one step