Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have alot of data in a dictionary format and I am attempting to use pandas print a string based on an IF ELSE statement. For my example ill make up some data in dict and covert to Pandas:

df = pd.DataFrame(dict(a=[1.5,2.8,9.3],b=[7.2,3.3,4.9],c=[13.1,4.9,15.9],d=[1.1,1.9,2.9]))

df

This returns:

    a   b   c   d

0   1.5 7.2 13.1 1.1

1   2.8 3.3 4.9 1.9

2   9.3 4.9 15.9 2.9

My IF ELSE statement:

for col in df.columns:

    if (df[col] < 4).any():

        print('Zone %s does not make setpoint' % col)

    else:

        print('Zone %s is Normal' % col)

Returns:

Zone a does not make setpoint

Zone b does not make setpoint

Zone c is Normal

Zone d does not make setpoint

But now I want to add in an extra to create a box plot where I am not making setpoint and also average the data frame where it is making setpoint. I know this is pandas series, but can pandas.Series.plot.box() be used?

This is my IF ELSE statement that I am using in a function with df.apply(lamba x:) and I am stuck trying to get the box box plot to work in pandas series... Any advice is greatly appreciated!

import matplotlib.pyplot as plt

def _print(x):

    if (x < 4).any():

        print('Zone %s does not make setpoint' % x.name)

        df.boxplot()

        plt.show()

    else:

        print('Zone %s is Normal' % x.name)

        print('The average is %s' % x.mean())

Im getting an error when I am calling df.apply(lambda x: _print(x))

module 'matplotlib' has no attribute 'show'

1 Answer

0 votes
by (41.4k points)

To get the boxplot of your column a, you can call pandas.Series.plot.box() like df['a'].plot.box() .

So, you can do this for your question:

def _print(x):

    if (x < 4).any():

        print('Zone %s does not make setpoint' % x.name)

        df[x.name].plot.box() #call x.name to retrieve the column name

        plt.show()

        print(df[x.name].describe())

    else:

        print('Zone %s is Normal' % x.name)

        print('The average is %s' % x.mean())

    print('---')

 

df.apply(lambda x: _print(x))

If you wish to learn more about how to use python for data science, then go through this data science python course by Intellipaat for more insights.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...