Python Forum
Python Pandas Syntax problem? Wrong Output, any ideas?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Pandas Syntax problem? Wrong Output, any ideas?
#1
Hello!

I was hoping to get some input on why my code is not working as intended. I want to produce the mean and median value of a Pandas series that contains the input from a previously read in Excel file, but I believe there is an error in my method. Any assistance would be much appreciated.

Here is my code:
df = pd.read_csv('train.csv')
id_values = pd.Series(df['PassengerId'])      

PassengerMedian = pd.Series.median(df['PassengerId'])
PassengerMean = pd.Series.mean(df['PassengerId'])

print(f"ID mean is {PassengerMean}; median value is :{PassengerMedian}")

display(id_values)
The problem is that as well as showing the values of PassengerId it says "ID mean is 446.0; median value is :446.0 ". Any hints on how to fix this? Cheers!
buran write Jan-18-2023, 09:48 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Where is the "Syntax problem" mentioned in the title?

I do not understand this statement:
Quote:The problem is that as well as showing the values of PassengerId it says "ID mean is 446.0; median value is :446.0 "
Your program contains a print statement that prints "ID mean is 446.0; median value is :446.0 ". If you don't want to print the message, why do you include the print statement in your program?

I could understand if you wanted to get rid of this message that is printed after the list of passenger id's:
Output:
Name: PassengerId, dtype: int64
Or is display(id_values) a special function that pretty prints the series?
Reply
#3
mean and median can give same output it depends on input data.
Read a little about the diffrence.
You can also call it directly on id_values.mean().
Example.
import pandas as pd

data = {'PassengerId': (2, 3, 3, 4, 6, 8, 9)}
df = pd.DataFrame(data)
>>> id_values = pd.Series(df['PassengerId'])
>>> id_values.mean()
5.0
>>> id_values.median()
4.0
If input data is like this then vaules is the same.
import pandas as pd

data = {'PassengerId': (1, 2, 3, 4, 5, 6, 7)}
df = pd.DataFrame(data)
>>> id_values = pd.Series(df['PassengerId'])
>>> id_values.mean()
4.0
>>> id_values.median()
4.0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 375 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  output shape problem with np.arange alan6690 5 697 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  problem in output of a function akbarza 9 1,206 Sep-29-2023, 11:13 AM
Last Post: snippsat
  pandas : problem with conditional filling of a column Xigris 2 636 Jul-22-2023, 11:44 AM
Last Post: Xigris
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,564 Mar-27-2023, 07:38 AM
Last Post: buran
  Am I wrong or is Udemy wrong? String Slicing! Mavoz 3 2,565 Nov-05-2022, 11:33 AM
Last Post: Mavoz
  Facing problem with Pycharm - Not getting the expected output amortal03 1 862 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  Os command output in variable shows wrong value paulo79 2 1,514 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  Help with coding/ideas natalie 6 1,852 Feb-12-2022, 03:16 PM
Last Post: deanhystad
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 4,312 Aug-29-2021, 12:39 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020