【发布时间】:2021-03-29 08:01:21
【问题描述】:
我有两个数据框,如下图,一个是df,另一个是异常:-
d = {'10028': [0], '1058': [25], '20120': [29], '20121': [22],'20122': [0], '20123': [0], '5043': [0], '5046': [0]}
df = pd.DataFrame(data=d)
df 的镜像副本中的基本异常值将是 0 或 1,表示值为 1 的异常和值为 0 的非异常
d = {'10028': [0], '1058': [1], '20120': [1], '20121': [0],'20122': [0], '20123': [0], '5043': [0], '5046': [0]}
anomalies = pd.DataFrame(data=d)
我正在使用以下代码将其转换为特定格式:-
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].'
)
问题是列值在每次运行中总是在变化我的意思是在这次运行中它的'10028', '1058', '20120', '20121', '20122', '20123', '5043', '5046' 但也许在下一次运行中它将是'10029', '1038', '20121', '20122', '20123', '5083', '5946'
如何根据数据框中存在的列动态创建详细信息,因为我不想硬编码,并且在消息中我想传递值为 1 的列的名称。
列的值将始终为 1 或 0。
【问题讨论】:
标签: python python-3.x pandas dataframe numpy