2

I have two data frame like below one is df and another one is anomalies:-

d = {'10028': [0], '1058': [25], '20120': [29], '20121': [22],'20122': [0], '20123': [0], '5043': [0], '5046': [0]}
    
    df = pd.DataFrame(data=d)

Basically anomalies in a mirror copy of df just in anomalies the value will be 0 or 1 which indicates anomalies where value is 1 and non-anomaly where value is 0

d = {'10028': [0], '1058': [1], '20120': [1], '20121': [0],'20122': [0], '20123': [0], '5043': [0], '5046': [0]}

anomalies = pd.DataFrame(data=d)

enter image description here

and I am converting that into a specific format with the below code:-

details = (
            '\n' + 'Metric Name' + '\t' + 'Count' + '\t' + 'Anomaly' +
            '\n' + '10028:' + '\t' + str(df.tail(1)['10028'][0]) + '\t' + str(anomalies['10028'][0]) + 
            '\n' + '1058:' + '\t' + '\t' + str(df.tail(1)['1058'][0]) + '\t' + str(anomalies['1058'][0]) + 
            '\n' + '20120:' + '\t' + str(df.tail(1)['20120'][0]) + '\t' + str(anomalies['20120'][0]) + 
            '\n' + '20121:' + '\t' + str(round(df.tail(1)['20121'][0], 2)) + '\t' + str(anomalies['20121'][0]) + 
            '\n' + '20122:' + '\t' + str(round(df.tail(1)['20122'][0], 2)) + '\t' + str(anomalies['20122'][0]) +
            '\n' + '20123:' + '\t' + str(round(df.tail(1)['20123'][0], 3)) + '\t' + str(anomalies['20123'][0]) +
            '\n' + '5043:' + '\t' + str(round(df.tail(1)['5043'][0], 3)) + '\t' + str(anomalies['5043'][0]) +
            '\n' + '5046:' + '\t' + str(round(df.tail(1)['5046'][0], 3)) + '\t' + str(anomalies['5046'][0]) +
            '\n\n' + 'message:' + '\t' +
            'Something wrong with the platform as there is a spike in [values where anomalies == 1].'
                )

The problem is the column values are changing always in every run I mean like in this run its '10028', '1058', '20120', '20121', '20122', '20123', '5043', '5046' but maybe in next run it will be '10029', '1038', '20121', '20122', '20123', '5083', '5946'

How I can create the details dynamically depending on what columns are present in the data frame as I don't want to hard code and in the message i want to pass the name of columns whose value is 1.

The value of columns will always be either 1 or 0.

1 Answer 1

2

Try this:

# first part of the string
s = '\n' + 'Metric Name' + '\t' + 'Count' + '\t' + 'Anomaly' 

# dynamically add the data
for idx, val in df.iloc[-1].iteritems():
    s += f'\n{idx}\t{val}\t{anomalies[idx][0]}' 
    # for Python 3.5 and below, use this
    # s += '\n{}\t{}\t{}'.format(idx, val, anomalies[idx][0])
    
# last part
s += ('\n\n' + 'message:' + '\t' +
      'Something wrong with the platform as there is a spike in [values where anomalies == 1].'
     )
Sign up to request clarification or add additional context in comments.

5 Comments

it gives me error:- File "<ipython-input-152-616df6ea1447>", line 6 s += f'\n{idx}\t{val}\t{anomalies[idx][0]}' ^ SyntaxError: invalid syntax i am using python 3 if that related to any error
the f string format is available from Python 3.6+. If you use earlier version, you need to change it to conventional format function.
Thanks a lot for your time!
Hey @Quang, Any idea how I can include a third data frame in this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.