BTM
BTM

Reputation: 49

How to convert output from python for loop into a table?

I have written a python code that generates output in the following format:

m=10 n=15 p=0.3 T=0.45

m=10 n=25 p=0.5 T=0.5

m=15 n=15 p=0.3 T=0.65

m=15 n=25 p=0.5 T=0.66

m=20 n=15 p=0.3 T=0.15

.......

I have around 1000 such rows. I want to get the output in a table format so that I can directly copy this into rows and columns of an excel file. Kindly help.

Upvotes: 1

Views: 1647

Answers (5)

sabhya
sabhya

Reputation: 1

import pandas as pd

# Create a DataFrame from the provided data
data = {
    "Household_Size": [5, 4, 4, 4, 5, 6, 6, 4, 5, 5, 3, 3, 4, 4, 4, 5, 4, 5, 5, 5, 4, 4, 5, 3, 1, 1, 2, 2, 1, 1, 3, 2, 2, 3, 3, 2, 3, 5, 5, 4, 5, 4, 4, 4, 4, 4, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 2, 2, 3, 4, 1, 1, 3, 3, 3, 3, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 6, 6, 4, 5, 6, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
    "Level_of_Education": [14, 14, 17, 16, 16, 17, 15, 17, 15, 13, 15, 15, 12, 17, 15, 15, 15, 17, 17, 15, 12, 10, 12, 14, 15, 13, 13, 13, 15, 13, 13, 13, 15, 15, 15, 12, 15, 15, 10, 12, 13, 15, 15, 12, 15, 15, 15, 15, 15, 15, 15, 12, 12, 14, 15, 15, 12, 17, 17, 16, 14, 15, 13, 15, 17, 16, 14, 15, 13, 15, 15, 14, 13, 15, 15, 17, 12, 14, 15, 12, 12, 14, 15, 15, 13, 15, 17, 17, 10, 12, 14, 14, 15, 13, 13, 14, 15, 15, 15],
}

df = pd.DataFrame(data)

# Define income groups based on quartiles of household size
q1 = df['Household_Size'].quantile(0.25)
q3 = df['Household_Size'].quantile(0.75)

def categorize_income_group(size):
    if size <= q1:
        return "Low Income"
    elif size <= q3:
        return "Middle Income"
    else:
        return "High Income"

# Categorize household size into income groups
df['Income_Group'] = df['Household_Size'].apply(categorize_income_group)

# Create a contingency table
contingency_table = pd.crosstab(df['Level_of_Education'], df['Income_Group'])

print("Contingency Table:")
print(contingency_table)

Upvotes: 0

Cesar Lopes
Cesar Lopes

Reputation: 413

I think as @Batselot told, the best way to do that would be with DataFrames. You can do one of the two following procedures:

1 - concatenate all the output values of your function in a single string, and then to the following code, which will treat the string, removing the '\n' and ' ':

values = """m=10 n=15 p=0.3 T=0.45

m=10 n=25 p=0.5 T=0.5

m=15 n=15 p=0.3 T=0.65

m=15 n=25 p=0.5 T=0.66

m=20 n=15 p=0.3 T=0.15"""

str_result = values.split('\n')
while '' in str_result:
    str_result.remove('')
data = []
for row in str_result:
    data.append(row.split(' '))
df = pd.DataFrame(data, columns=['m', 'n', 'p', 'T'])
#If my excel file were called 'excel_file.xlsx' and it is in the same folder of my python code and it had a sheet called 'Sheet1'.
df.to_excel(r"excel_file.xlsx", sheet_name='Sheet1', index=False)

2 - You can just append the values straight in a list inside your loop, and you wont have to treat it with string manipulation.

data = []
#You Probably have a for loop like this:
for i in 'some list':
    print("m={}".format(m), "m={}".format(n), "m={}".format(p), "m={}".format(T))
    data.append(["m={}".format(m), "n={}".format(n), "p={}".format(p), "T={}".format(T)])

df = pd.DataFrame(data, columns=['m', 'n', 'p', 'T'])
print(df)
#If my excel file were called 'excel_file.xlsx' and it is in the same folder of my python code and it had a sheet called 'Sheet1'.
df.to_excel(r"excel_file.xlsx", sheet_name='Sheet1', index=False)

Note 1: your excell sheet must be closed if not you will receive a permission error.

Note 2: you’ll have to install openpyxl if you get the following error:

ModuleNotFoundError: No module named ‘openpyxl’

You may then use PIP to install openpyxl as follows:

pip install openpyxl

Upvotes: 1

Bisakh Mondal
Bisakh Mondal

Reputation: 63

I wrote a simple script in python, that should do your job.

import sys

print("m,n,p,T")
# read till EOF os stdin
for line in sys.stdin:
    splits = line.rstrip().split(" ")
    if len(splits) != 4:
        continue
    vals = [(lambda x: x.split("=")[1])(x) for x in splits]
    if len(vals) != 4:
        continue
    print(f'{vals[0]},{vals[1]},{vals[2]},{vals[3]}')

It will format the output of your program in csv format which you can load in excel or a spreadsheet.

To do this,

  1. store this script in something like script.py
  2. run this in shell
$ python <your program>.py | python script.py > output.csv
  1. The result will be stored in output.csv which looks like
m,n,p,T
10,25,0.5,0.5
15,15,0.3,0.65
15,25,0.5,0.66
20,15,0.3,0.15

This csv can be loaded into excel.

Upvotes: 0

Dr. V
Dr. V

Reputation: 1914

The easiest is to initialize a list upfront and append each data point to that list:

result = []

for i in range(1, 10):
    # ... do your calculations
    result.append([i, i*i, i*i*i, 1/i, 1/i/i])

Edit:

What I also sometimes did in the past is to write out the table in html-table format:

<table>
  <tr><td>1</td><td>2</td><td>3</td></tr>
  <tr><td>4</td><td>5</td><td>6</td></tr>
</table>

Normally, writing to an Excel file directly (see the answer from Batselot) can be the better solution. The html-trick allows you to paste the data into an open Excel file where you like to have it.

Upvotes: 0

Batselot
Batselot

Reputation: 303

The best way to approach this situation in my opinion is to use dataframes. These dataframes can help you make a table with column names and etc. Pandas can help with you that which is one of the most convenient libraries to use. Here is a quick pseudocode I hope it helps.

import pandas as pd 
list=[]
list.append(["Your desired result"])
df=pd.Dataframe(list)
pd.to_excel(df)

Upvotes: 1

Related Questions