我需要根据规则创建一个新列,其中包含字母{a,b,c,d}:
{'a' if (df['q1']==0 & df['q2']==0),
'b' if (df['q1']==0 & df['q2']==1),
'c' if (df['q1']==1 & df['q2']==0),
'd' if (df['q1']==1 & df['q2']==1)}
所以,新的第三列应该包含一个字母,该字母对应于两列中{0,1}的特定组合。
q1 q2
0 0 1
1 0 1
2 0 1
3 0 1
4 0 1
5 0 1
6 0 1
7 0 1
8 0 1
9 0 1
10 1 1
11 1 1
12 0 1
13 0 1
14 1 0
15 0 0
16 0 0
17 0 0
18 0 0
19 0 0
20 0 0
21 0 0
我考虑过将每行中的数字从二进制格式转换为十进制格式,然后应用字典规则。
答案 0 :(得分:3)
您可以Series
使用MultiIndex
idx = pd.MultiIndex.from_product([[0,1],[0,1]], names=('q1','q2'))
s = pd.Series(['a','b','c','d'], index=idx, name='val')
print (s)
q1 q2
0 0 a
1 b
1 0 c
1 d
Name: val, dtype: object
df = df.join(s, on=['q1','q2'])
print (df)
q1 q2 val
0 0 1 b
1 0 1 b
2 0 1 b
3 0 1 b
4 0 1 b
5 0 1 b
6 0 1 b
7 0 1 b
8 0 1 b
9 0 1 b
10 1 1 d
11 1 1 d
12 0 1 b
13 0 1 b
14 1 0 c
15 0 0 a
16 0 0 a
17 0 0 a
18 0 0 a
19 0 0 a
20 0 0 a
21 0 0 a
:
<ion-navbar [hideBackButton]="hide">
<ion-navbar hideBackButton="{{ hide }}">
答案 1 :(得分:1)
使用df.transform
和In [90]: mapping = {(0, 0) :'a', (0, 1) : 'b', (1, 0): 'c', (1, 1): 'd'}
In [91]: df['val'] = df.transform(lambda x: (x['q1'], x['q2']), axis=1).map(mapping); df
Out[91]:
q1 q2 val
0 0 1 b
1 0 1 b
2 0 1 b
3 0 1 b
4 0 1 b
5 0 1 b
6 0 1 b
7 0 1 b
8 0 1 b
9 0 1 b
10 1 1 d
11 1 1 d
12 0 1 b
13 0 1 b
14 1 0 c
15 0 0 a
16 0 0 a
17 0 0 a
18 0 0 a
19 0 0 a
20 0 0 a
21 0 0 a
的另一种方法:
zip
您还可以使用pd.Series
生成列,应用In [119]: df['val'] = pd.Series(list(zip(df.q1, df.q2))).map(mapping); df
Out[119]:
q1 q2 val
0 0 1 b
1 0 1 b
2 0 1 b
3 0 1 b
4 0 1 b
5 0 1 b
6 0 1 b
7 0 1 b
8 0 1 b
9 0 1 b
10 1 1 d
11 1 1 d
12 0 1 b
13 0 1 b
14 1 0 c
15 0 0 a
16 0 0 a
17 0 0 a
18 0 0 a
19 0 0 a
20 0 0 a
21 0 0 a
然后执行映射:
In [552]: %%timeit
...: idx = pd.MultiIndex.from_product([[0,1],[0,1]], names=('q1','q2'))
...: s = pd.Series(['a','b','c','d'], index=idx, name='val')
...: df.join(s, on=['q1','q2'])
...:
100 loops, best of 3: 2.84 ms per loop
In [553]: %%timeit
...: mapping = {(0, 0) :'a', (0, 1) : 'b', (1, 0): 'c', (1, 1): 'd'}
...: df.transform(lambda x: (x['q1'], x['q2']), axis=1).map(mapping)
...:
1000 loops, best of 3: 1.7 ms per loop
在这篇文章中提出:
var data_table, row_num = 1,
col_num = 11,
row_cell = 1,
col_cell = 0,
iter = 0;
var cols = [];
var data = [{
"StopName": "Udhna Darwaza",
"1": "07:01:00",
"2": "07:16:00",
"3": "07:31:00",
"4": "07:46:00",
"5": "08:01:00",
"6": "08:16:00",
"7": "08:31:00",
"8": "08:46:00",
"9": "09:01:00",
"10": "09:16:00"
}, {
"StopName": "Kharwar Nagar",
"1": "07:02:00",
"2": "07:17:00",
"3": "07:32:00",
"4": "07:47:00",
"5": "08:02:00",
"6": "08:17:00",
"7": "08:32:00",
"8": "08:47:00",
"9": "09:02:00",
"10": "09:17:00"
}, {
"StopName": "Satya Nagar",
"1": "07:03:00",
"2": "07:18:00",
"3": "07:33:00",
"4": "07:48:00",
"5": "08:03:00",
"6": "08:18:00",
"7": "08:33:00",
"8": "08:48:00",
"9": "09:03:00",
"10": "09:18:00"
}, {
"StopName": "Udhna Tran Rasta",
"1": "07:04:00",
"2": "07:19:00",
"3": "07:34:00",
"4": "07:49:00",
"5": "08:04:00",
"6": "08:19:00",
"7": "08:34:00",
"8": "08:49:00",
"9": "09:04:00",
"10": "09:19:00"
}, {
"StopName": "Udhna Bus Stand",
"1": "07:05:00",
"2": "07:20:00",
"3": "07:35:00",
"4": "07:50:00",
"5": "08:05:00",
"6": "08:20:00",
"7": "08:35:00",
"8": "08:50:00",
"9": "09:05:00",
"10": "09:20:00"
}, {
"StopName": "Udhna Academy",
"1": "07:06:00",
"2": "07:21:00",
"3": "07:36:00",
"4": "07:51:00",
"5": "08:06:00",
"6": "08:21:00",
"7": "08:36:00",
"8": "08:51:00",
"9": "09:06:00",
"10": "09:21:00"
}, {
"StopName": "Laxmi Narayan Temple",
"1": "07:07:00",
"2": "07:22:00",
"3": "07:37:00",
"4": "07:52:00",
"5": "08:07:00",
"6": "08:22:00",
"7": "08:37:00",
"8": "08:52:00",
"9": "09:07:00",
"10": "09:22:00"
}, {
"StopName": "Daksheswar Mahadev",
"1": "07:08:00",
"2": "07:23:00",
"3": "07:38:00",
"4": "07:53:00",
"5": "08:08:00",
"6": "08:23:00",
"7": "08:38:00",
"8": "08:53:00",
"9": "09:08:00",
"10": "09:23:00"
}, {
"StopName": "Pandesara G.I.D.C.",
"1": "07:09:00",
"2": "07:24:00",
"3": "07:39:00",
"4": "07:54:00",
"5": "08:09:00",
"6": "08:24:00",
"7": "08:39:00",
"8": "08:54:00",
"9": "09:09:00",
"10": "09:24:00"
}, {
"StopName": "Kamnath Mahadev",
"1": "07:10:00",
"2": "07:25:00",
"3": "07:40:00",
"4": "07:55:00",
"5": "08:10:00",
"6": "08:25:00",
"7": "08:40:00",
"8": "08:55:00",
"9": "09:10:00",
"10": "09:25:00"
}, {
"StopName": "Navin Flourine",
"1": "07:11:00",
"2": "07:26:00",
"3": "07:41:00",
"4": "07:56:00",
"5": "08:11:00",
"6": "08:26:00",
"7": "08:41:00",
"8": "08:56:00",
"9": "09:11:00",
"10": "09:26:00"
}, {
"StopName": "Bhestan Naher",
"1": "07:12:00",
"2": "07:27:00",
"3": "07:42:00",
"4": "07:57:00",
"5": "08:12:00",
"6": "08:27:00",
"7": "08:42:00",
"8": "08:57:00",
"9": "09:12:00",
"10": "09:27:00"
}, {
"StopName": "Bhagwati Industrial Estate",
"1": "07:13:00",
"2": "07:28:00",
"3": "07:43:00",
"4": "07:58:00",
"5": "08:13:00",
"6": "08:28:00",
"7": "08:43:00",
"8": "08:58:00",
"9": "09:13:00",
"10": "09:28:00"
}, {
"StopName": "Unn Naka",
"1": "07:14:00",
"2": "07:29:00",
"3": "07:44:00",
"4": "07:59:00",
"5": "08:14:00",
"6": "08:29:00",
"7": "08:44:00",
"8": "08:59:00",
"9": "09:14:00",
"10": "09:29:00"
}, {
"StopName": "Unn Industrial Estate",
"1": "07:15:00",
"2": "07:30:00",
"3": "07:45:00",
"4": "08:00:00",
"5": "08:15:00",
"6": "08:30:00",
"7": "08:45:00",
"8": "09:00:00",
"9": "09:15:00",
"10": "09:30:00"
}, {
"StopName": "Unn Char Rasta",
"1": "07:16:00",
"2": "07:31:00",
"3": "07:46:00",
"4": "08:01:00",
"5": "08:16:00",
"6": "08:31:00",
"7": "08:46:00",
"8": "09:01:00",
"9": "09:16:00",
"10": "09:31:00"
}, {
"StopName": "Green Park",
"1": "07:17:00",
"2": "07:32:00",
"3": "07:47:00",
"4": "08:02:00",
"5": "08:17:00",
"6": "08:32:00",
"7": "08:47:00",
"8": "09:02:00",
"9": "09:17:00",
"10": "09:32:00"
}, {
"StopName": "Sachin G.I.D.C.",
"1": "07:18:00",
"2": "07:33:00",
"3": "07:48:00",
"4": "08:03:00",
"5": "08:18:00",
"6": "08:33:00",
"7": "08:48:00",
"8": "09:03:00",
"9": "09:18:00",
"10": "09:33:00"
}, {
"StopName": "Sachin GIDC Naka",
"1": "07:30:00",
"2": "07:45:00",
"3": "08:00:00",
"4": "08:15:00",
"5": "08:30:00",
"6": "08:45:00",
"7": "09:00:00",
"8": "09:15:00",
"9": "09:30:00",
"10": "09:45:00"
}];
//Get stored data from HTML table element
var results = [];
function initDT() {
debugger;
//Construct the measurement table
data_table = $('#example').dataTable({
"bJQueryUI": true,
"bDeferRender": true,
"bInfo": false,
"bSort": false,
"bDestroy": true,
"bFilter": false,
"bPagination": false,
"aaData": results,
"aoColumns": cols,
});
attachTableClickEventHandlers();
}
function fnCreatedRow(nRow, aData, iDataIndex) {
var sURL = aData[6];
var t = "<img src='" + sURL + "' width='90' height='126'>";
$(nRow).attr('tooltip-title', t);
}
function fnDrawCallback(settings) {
$(".tooltip-card-name").each(function(i, td) {
var title = $(td).parent().attr('tooltip-title');
$(td).tooltip({
"delay": 0,
"track": true,
"fade": 250,
content: title
})
});
}
function GetSchedules() {
cols = [];
var returnedData = data;
if (returnedData.length > 0) {
var obj = returnedData[0];
for (var key in obj) {
var attrName = key;
var attrValue = obj[key];
var column = {
"mData": key,
"mDataProp": key,
"sTitle": key,
"sType": "string",
"fnCreatedCell": function(nTd, sData, oData, iRow, iCol) {
if (iCol == 0) {
nTd.title = $(nTd).text();
if ($(nTd).text().length > 3) {
$(nTd).text($(nTd).text().substring(0, 3).concat("..."));
}
}
}
};
cols.push(column);
}
}
debugger;
results = returnedData;
//$('#example').dataTable().fnClearTable();
initDT();
addDBClikHandler();
}
function attachTableClickEventHandlers() {
//row/column indexing is zero based
$("#example thead tr th").click(function() {
col_num = parseInt($(this).index());
console.log("column_num =" + col_num);
});
$("#example tbody tr td").click(function() {
col_cell = parseInt($(this).index());
row_cell = parseInt($(this).parent().index());
console.log("Row_num =" + row_cell + " , column_num =" + col_cell);
});
};
$("#btnAddRow").click(function() {
//adding/removing row from datatable datasource
//create test new record
var aoCols = data_table.fnSettings().aoColumns;
var newRow = new Object();
for (var iRec = 0; iRec < aoCols.length; iRec++) {
if (aoCols[iRec]._sManualType === "date") {
newRow[aoCols[iRec].mDataProp] = "2011/03/25";
} else if (aoCols[iRec]._sManualType === "numeric") {
newRow[aoCols[iRec].mDataProp] = 10;
} else if (aoCols[iRec]._sManualType === "string") {
newRow[aoCols[iRec].mDataProp] = 'testStr';
}
}
results.splice(row_cell + 1, 0, newRow);
data_table.fnDestroy();
initDT();
addDBClikHandler();
});
$('#btnAddCol').click(function() {
//new column information
//row's new field(for new column)
//cols must be updated
cols.splice(col_num + 1, 0, {
"mDataProp": "newField" + iter,
sTitle: "Col-" + iter,
sType: "string"
});
//update the result, actual data to be displayed
for (var iRes = 0; iRes < results.length; iRes++) {
results[iRes]["newField" + iter] = "data-" + iter;
}
//destroy the table
data_table.fnDestroy();
$("#example thead tr th").eq(col_num).after('<th>Col-' + iter + '</th>');
//init again
initDT();
iter++;
addDBClikHandler();
});
function restoreRow(oTable, nRow) {
var aData = oTable.fnGetData(nRow);
var jqTds = $('>td', nRow);
for (var i = 0, iLen = jqTds.length; i < iLen; i++) {
oTable.fnUpdate(aData[i], nRow, i, false);
}
};
function editRow(oTable, nRow) {
var aData = oTable.fnGetData(nRow);
var jqTds = $('>td', nRow);
jqTds[col_cell].innerHTML = '<input type="text" id ="typ" value="' + aData[cols[col_cell].mData] + '"/>';
$("#typ").autocomplete({
source: type1
});
};
function saveRow(oTable, nRow) {
var jqInputs = $('input', nRow);
oTable.fnUpdate(jqInputs[0].value, row_cell, col_cell, false);
};
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
"date-uk-pre": function(a) {
var ukDatea = a.split('/');
return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1
},
"date-uk-asc": function(a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-uk-desc": function(a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});
/* Get the rows which are currently selected */
function fnGetSelected(oTableLocal) {
var aReturn = new Array();
var aTrs = oTableLocal.fnGetNodes();
for (var i = 0; i < aTrs.length; i++) {
if ($(aTrs[i]).hasClass('row_selected')) {
aReturn.push(aTrs[i]);
}
}
return aReturn;
};
function addDBClikHandler() {
$('#example tbody tr').on('dblclick', function(e) {
e.preventDefault();
var nRow = $(this)[0];
var jqTds = $('>td', nRow);
if ($.trim(jqTds[0].innerHTML.substr(0, 6)) != '<input') {
if (nEditing !== null && nEditing != nRow) {
/* Currently editing - but not this row - restore the old before continuing to edit mode */
restoreRow(oTable, nEditing);
nEditing = nRow;
editRow(oTable, nRow);
} else {
/* No edit in progress - let's start one */
nEditing = nRow;
editRow(oTable, nRow);
}
}
});
$('#example tbody tr').keydown(function(event) {
if (event.keyCode == 13) {
event.preventDefault();
if (nEditing == null)
alert("Select Row");
else
saveRow(oTable, nEditing);
nEditing = null;
}
/* Editing this row and want to save it */
});
};
var nEditing = null;
var oTable = null;
$(document).ready(function() {
GetSchedules();
initDT();
oTable = data_table;
addDBClikHandler();
});