Home > Software engineering >  Select how much data the pd.mean() displays?
Select how much data the pd.mean() displays?

Time:05-06

When returning the pd.mean(), how to show only specific information?

Example

Now it gives this:

new_df = pd.read_excel('example.xls', usecols = ['weight'] )
print('avg value for column weight is here: '  str(data.mean()))

Result

avg value for column weight is here: weight 2311.000000 dtype: float64

it should return:

avg value for column weight is here: 2311.000000

CodePudding user response:

For avoid one value Series select column weight. If use Series.mean - call function for one column get scalar output:

print('avg value for column weight is here: '  str(data['weight'].mean()))

Or use f-strings for same ouput:

print(f'avg value for column weight is here: {data["weight"].mean()}')

CodePudding user response:

Instead of data, which returns mean values for the dataframe,using df['weight'] or df.weight,

will return the mean of just the dataframe column. df['weight'] is basically slicing the dataframe to return a series, which means you're specifying exactly what you want the mean of, so the column name value isn't returned.

fstrings are a useful way to change this if there was no other way to alter the input, as they force an output into a string value. This would have to be coupled with using [] slice notation to return only the specific characters from the string that you want displayed

you literally just add an f"" around your input and {} around any field that needs replacing with a string

print(f"avg value for column weight is here: {data["weight"].mean()}")
  • Related