Customer_id Name Age Balance
Q1 True True True
W2 True True True
E3 True False True
T5 True True False
Y6 True True True
U7 True True True
I8 False False False
O9 True False False
P0 False False False
我想在上述数据框中以黄色突出显示或用黄色填充单词'TRUE'
这是我尝试的代码:
def color_negative_red(val):
color = 'yellow' if val == 'TRUE' else 'black'
return 'color: %s' % color
df = dataframe.style.\
apply(color_negative_red).\
to_excel('df.xlsx')
我收到以下错误
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Customer_id')
我在这里做什么错了?
答案 0 :(得分:2)
使用Styler.applymap
代替apply
:
dataframe.style.\
applymap(color_negative_red).\
to_excel('df.xlsx')
如果是布尔值,也可以用True
进行比较,如果是字符串,还可以用'True'
进行比较:
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: %s' % color
#python 3.6+ with f-strings
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return f'color: {color}'
#python bellow 3.6
def color_negative_red(val):
color = 'yellow' if val == True else 'black'
return 'color: {}'.format(color)
如果还要删除索引值:
dataframe.style.\
applymap(color_negative_red).\
to_excel('df.xlsx', index=False)
答案 1 :(得分:0)
尝试一下(这里可以使用<?php
require("phpMQTT.php");
$server = "xx.xx.xx.xx";
$port = 1883;
$username = "username";
$password = "password";
$client_id = uniqid();
$mqtt = new phpMQTT($server, $port, $client_id);
//Read message from form, which type of sensor to read
$msg = $_POST['sensor'];
if (!empty($msg)) {
if ($mqtt->connect(true, null, $username, $password)) {
//Publish type of sensor to read
$mqtt->publish("dev/sensors", $msg , 0);
$mqtt->close();
}
//Start listen for response
subscribeToTopic($mqtt);
}
function subscribeToTopic($mqtt)
{
//Set topic to listen to
$topics['dev/sensors'] = array("qos" => 0, "function" => "procmsg");
$mqtt->subscribe($topics, 0);
//Listen for the response in the subscribed topic
while ($mqtt->proc()) {
}
$mqtt->close();
}
function procmsg($topic, $msg)
{
global $mqtt;
echo $msg;
//Close mqtt-connection after message is received
$mqtt->close();
}
?>
):
apply