我有一个如下所示的df:
time a b
0 0.000 6 5
1 0.008 6 9
2 0.016 1 9
3 0.024 2 7
4 0.032 1 5
我想使用df.a和df.b中的每个值作为以下向量中的索引:
x = [-6, -4, -3, -2, -1, 0.5, 1, 2, 4, 6]
结果将创建2个新列df.a_和df.b_,它们显示x中的值,其中df.a或df.b对其进行索引。另外,在索引以说明第0个元素时,我想从每个df.a和df.b值中减去1。它应该看起来像这样:
time a b a_ b_
0 0.000 6 5 0.5 -1
1 0.008 6 9 0.5 4
2 0.016 1 9 -6 4
3 0.024 2 7 -4 2
4 0.032 1 5 -6 -1
答案 0 :(得分:1)
看来您对class SessionStore : ObservableObject {
@Published var session: FIRUser?
var isLoggedIn: Bool { session != nil}
var handle: AuthStateDidChangeListenerHandle?
init () {
handle = Auth.auth().addStateDidChangeListener { (auth, user) in
if let user = user {
self.session = user
} else {
self.session = nil
}
}
}
deinit {
if let handle = handle {
Auth.auth().removeStateDidChangeListener(handle)
}
}
}
的索引已减1。但这是使用
struct AppView: View {
@ObservedObject var session = SessionStore()
var body: some View {
Group {
if session.isLoggedIn {
...
} else {
...
}
}
}
}
进行索引的快速方法。
@Published
答案 1 :(得分:1)
IIUC,这可以通过numpy索引解决:
ON [Sessions]
输出:
CREATE TABLE [Sessions]
(
--Field specifications and constraints--
RiderID int not null,
BikeID nchar(6) not null,
SessionDate datetime not null
constraint chk_Sessions_SessionDate check (SessionDate > '1 Sep 2017'), --SessionDate must be AFTER September 1, 2017
Laps int null,
--Table constraints--
constraint pk_Sessions_RiderID_BikeID_SessionDate primary key (RiderID, BikeID, SessionDate), --RiderID, BikeID, SessionDate is the composite primary key for the Sessions table
INDEX CI_Sessions_RiderID_BikeID (RiderID, BikeID) --Index RiderID and BikeID
)
答案 2 :(得分:1)
我也使用numpy花式索引,但是我会对所有列都这样做,并构造一个字典以与Assign一起使用。一次分配多个列的另一种方法。
a = np.array(x)
cols = ['a', 'b']
d = dict(zip(np.char.add(cols, '_'), a[df[cols] - 1].T))
df = df.assign(**d)
Out[721]:
time a b a_ b_
0 0.000 6 5 0.5 -1.0
1 0.008 6 9 0.5 4.0
2 0.016 1 9 -6.0 4.0
3 0.024 2 7 -4.0 1.0
4 0.032 1 5 -6.0 -1.0
答案 3 :(得分:0)
一个选项是apply
和lambda
df['a_'] = df['a'].apply(lambda y: x[y])
df['b_'] = df['b'].apply(lambda y: x[y])
time a b a_ b_
0 0.000 6 5 1 0.5
1 0.008 6 9 1 6.0
2 0.016 1 9 -4 6.0
3 0.024 2 7 -3 2.0
4 0.032 1 5 -4 0.5
答案 4 :(得分:0)
IIUC
/*
************************************
** Node.js requirments **
************************************
*/
var dgram = require("dgram");
var udpResultsServer = dgram.createSocket("udp4");
var udpTimeServer = dgram.createSocket("udp4");
var http = require('http');
var fs = require('fs');
var io = require('socket.io');
// Defining some variables
var htmlPage;
var jsonResults;
var tempResults = '';
var finalResults = '';
var time = '0.0';
var r = new Array();
/*
************************************
** UDP Results Server **
************************************
*/
// start UDP server listening on port 43278
udpResultsServer.on("listening", function () {
var address = udpResultsServer.address();
console.log("UDP server listening on port: " + address.port);
});
udpResultsServer.bind(43278);
// process datagram
udpResultsServer.on("message", function (msg, rinfo) {
// check if datagram is full (size=536)
if(rinfo.size == 536){
tempResults = tempResults + msg.toString();
}
// datagram is not full, so this is the end of the message
else{
finalResults = tempResults + msg.toString();
r = finalResults.split(';'); // split the final string by ';' and put in array r
for (var i = 0; i < r.length; i++) { // loop through each array element (result line)
var tmp = r[i].length; // check size, if it's 14 it is an empty result set
if (tmp == 14 || tmp == 0){
r.splice(i,1); // if empty remove from array
}
else {
r[i]= r[i].split(','); // if line is not empty split the line bt ','
}
}
r.unshift(time);
tempResults =''; // reset tempResults variable to prepare for next datagram
}
});
/*
************************************
** UDP Time Server **
************************************
*/
// start UDP server listening on port 43279
udpTimeServer.on("listening", function () {
var address = udpTimeServer.address();
console.log("UDP server listening on port: " + address.port);
});
udpTimeServer.bind(43279);
// process datagram
udpTimeServer.on("message", function (clock, rinfo) {
time = clock.toString();
time = time.trim();
r[0]=time;
});
/*
************************************
** Webserver and sockets.io **
************************************
*/
var htmlPage;
// read html file to serve
fs.readFile('lynx.html', function(error, data){
if (error){
throw error;
}
else{
htmlPage = data;
}
});
// start webserver
var htmlServer = http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.end(htmlPage);
});
htmlServer.listen(8000);
console.log("HTML server started on: " + getIP());
// start up socket.io for transmitting data to the webpage
var serv_io = io.listen(htmlServer, { log: false });
serv_io.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
jsonResults = JSON.stringify(r); // convert final array into a json string
socket.send(jsonResults); // sent json results via socket
}, 1000);
});
/*
************************************
** Get Ip Addresses **
************************************
*/
function getIP(){
var os = require( 'os' );
var networkConfig = os.networkInterfaces( );
var ipList ='';
for (var name in networkConfig) {
networks = networkConfig[name];
for (var name in networks) {
details = networks[name];
if (details['family'] == "IPv4"){
ipList = ipList + details['address'] + " ";
}
}
}
return ipList;
}