Home > Software design >  How to save multiple datas using pandas into excel
How to save multiple datas using pandas into excel

Time:07-05

I am trying to print the inputs and outputs of the below code into an excel sheet using pandas but I am not aware as how to write the multiple data values to the output file using for loop. Below is the code:

import random
import pandas as pd
def result(a,b):
    return a b

for i in range(10):
    a = random.randint(1,100)
    b = random.randint(1,100)
    res = result(a,b)
    col1 = 'num1'
    col2 = 'num2'
    col3 = 'result'
    data = pd.DataFrame({col1:[a],col2:[b],col3:[res]})
    data.to_excel('output.xlsx', sheet_name='sheet1', index=False)

When I run the above code I get only the final loop execution in the excel sheet. Please suggest me some ways. 1 is the expected output image.

CodePudding user response:

If you are open to using a technique where you don't need a for loop this will give you what you are looking for with 2 columns of random data between 1 and 100 with 10 rows

import pandas as pd
import numpy as np

#Random 2 columns of data between 1 and 100 with 10 rows
df = pd.DataFrame(np.random.randint(0,100,size=(10, 2)), columns=['Column1', 'Column2'])

#Sums column1 and 2 as result
df['Result'] = df['Column1'].add(df['Column2'])

#Saves to the excel file you specified
df.to_excel('output.xlsx', sheet_name='sheet1', index=False)

CodePudding user response:

In the loop that you have written, you are just updating a single value to each column in every iteration, and writing the whole excel again with just single row(i.e. in every iteration).

The logic is amiss here.

The below code is an easy to understand version of the same code with expected output:
import random
import pandas as pd
def result(a,b):
    return a b

col1 = 'num1'
col2 = 'num2'
col3 = 'result'
sample_dict = {col1: [], col2:[], col3:[]}
for i in range(10):
    a = random.randint(1,100)
    b = random.randint(1,100)
    res = result(a,b)
    sample_dict[col1].append(a)
    sample_dict[col2].append(b)
    sample_dict[col3].append(res)
    
output_df = pd.DataFrame(sample_dict)
output_df.to_excel('output.xlsx', sheet_name='sheet1', index=False)

CodePudding user response:

There is one mistake in your code. As you are writing the data into the pandas dataframe, everytime its getting overwritten by the last value, hence you are getting last value as output, and earlier values are not persisting. What i suggest, is to save the 3 values into a list and then creating a dataframe with this list of lists. Check out the code below:

import random
import pandas as pd
list_of_columns = []

def result(a,b):
    return a b

for i in range(10):
    a = random.randint(1,100)
    b = random.randint(1,100)
    res = result(a,b)
    list_of_columns.append([a,b,res])
    
data = pd.DataFrame(list_of_columns)
data.columns = ['num1', 'num2','result']
data.to_excel('output.xlsx', sheet_name='sheet1', index=False)

Hope this helps !

  • Related