Python | How to make a program that calculates strings


I'm trying to create a Python script using pandas that can import a .txt file and calculate the average of each subject

I'm trying to turn this "file.txt":

code name subject1 subject2 subject3
1234 Ali 6 0 8
1235 Carl 4 7 7
1236 Jason 3 5 0

and turn in intro this:

subject1 average is: 4.3
subject2 average is: 6
subject3 average is: 7.5
  • subject1 is calculated like this: (6 + 4 + 3) / 3,
  • subject2 is calculated like this: (7 + 5) / 2 <-- because one person has a 0 means he/she didn't anticipate so their 0 does't add and counts toward the average

  • subject3 is calculated like this: (8 + 7) / 2 <-- Like above

    I'm also trying to figure out a way for the script to be flexible and have the ability to add more subjects and more people (so 3 instead of 5)

This is my code until now:

# read input file
df = pd.read_csv('file.txt')

# calculate mean, ignoring 0 values
df['mean'] = df.iloc[:, 2:].astype(float).replace(0, np.nan).mean(1)

# iterate rows and print results
for name, mean in df.set_index('name')['mean'].items():
    print(f'{name} has average of {mean:.2f}')
  • It calculates the average of each person (horizontally)
  • but I can't figure out a way to do it vertically for each subject.

thanks for the help guys ^_^


The argument 1 that you provide to pd.Series.mean is the axis along which the mean is calculated; the default is columns, so you are explicitly telling it to calculate the row-wise mean. Remove that argument and you should be good.

In [155]: df.iloc[:, 2:].astype(float).replace(0, np.nan).mean()
subject1    4.333333
subject2    6.000000
subject3    7.500000

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


How to make a histogram from a list of strings in Python?

How can I make an EXE file from a Python program?

How To Make a Python Program Automatically Restart Itself

How to make this program in python?

How to make an interactive program?

Python 3: How to print a program that validates strings

How to make strings into a list?

How to make a program that can be opened with a file? (python)

How do I make my python program to write a new file

How to make a proper name input program in python

How to pass multiple strings to a python program

a PYTHON program that takes its input from a file of numbers and calculates the average

How to make a one-window program with GTK, python and glade?

PYTHON: How to make a program to stop if some seconds have passed?

how to make a tkinter GUI that calculates how much money you would make in a given time with python

How to make 'python' program command execute Python 3?

How to make python program ".py" executable?

Python IDLE shell - How to make multiline strings

How to make globally callable python program?

How to make a program run at startup using python

How to make a python program that lists the positions and displays and error message if not found

How to fix a program that incorrectly calculates the percentages of numbers in Java?

My program times out on large Strings, how could I make it faster?

How to make a program end on a blank line in python?

How to make a word counter program using Python?

How to make an on/off switch for a function in a python program?

How do you make a new variable while a program is running in python?

How to make any python program run standalone

How to make python strings inside lists into splitted strings

TOP Ranking