假设我有一个带有ID,月份和状态的pandas DataFrame。每个ID有几行,每行代表一个月以及给定月份的状态。我想找到具有所有可能状态的ID。我该怎么做?
async function createselbox(arr) {
console.log('In createselbox');
var startstr = `Something`;
mytemp = '';
for (var i = 0; i < arr.length; i++) {
mytemp = mytemp + '<option>' + arr[i] + '</option>';
}
var ret = startstr + mytemp + `Something else `;
let IsStaff = await CheckiStaff();
console.log('Checkifstaff is ' + IsStaff);
if (IsStaff) {
console.log('Got true createselbox');
console.log('In sel loop');
ret = startstr + mytemp + `Something more`;
} else {
console.log('Got false createselbox');
}
return ret;
}
function CheckifStaff() {
var data = {
"isstaff": 'check'
};
data = $(this).serialize() + "&" + $.param(data);
return new Promise((resolve, reject) => {
$.ajax({
type: "POST",
dataType: "html",
url: "/functions/checkifstaff",
data: data,
success: function (data) {
var recdata = data;
if (recdata.indexOf('Has add permissions') != -1) {
console.log('Returning true');
resolve(1);
} else {
console.log('Returning false');
resolve(0);
}
},
error: function (xhr, textStatus, errorThrown) {
console.log('An error occured in CheckifStaff');
resolve(0);
}
});
});
}
我希望找到输出12的代码,因为该ID包含所有可能的状态值(在这种情况下为'a'和'b')
答案 0 :(得分:0)
尝试:
>>> df2 = df.groupby('ID').aggregate(set)
>>> df2
Month Status
ID
11 {1, 2} {a}
12 {1, 2} {b, a}
>>> max_states = 2 # you should know that
>>> list(df2[df2.apply(lambda x: len(x['Status'])==max_states, axis=1)].index)
[12]
答案 1 :(得分:0)
您可以通过使用crosstab
然后仅考虑具有所有非零元素的行来实现此目的:
#Creates a crosstab the variable 'Status'
df1 = pd.crosstab(df['ID'], df['Status'])
#Considers only rows where all values are non-zero
df1 = df1[(df1.T != 0).all()]
print(df1)
Status a b
ID
12 1 1