How to group this Time column based on some interval and create new column based on it and assign new simpler values to it - data-science

This is the dataframe image where in from Time column I am trying to make some group intervals and assigning some simple values on the Time_group column
This is the code I am trying:
for i in sales_df['Time'].str[:2]:
if (i == '10') & (i == '11') & (i == '12'):
sales_df['Time_group'] = 1
if (i == '13') & (i == '14') & (i == '15'):
sales_df['Time_group'] = 2
if (i == '16') & (i == '17') & (i == '18'):
sales_df['Time_group'] = 3
if (i == '19') & (i == '20') & (i == '21'):
sales_df['Time_group'] = 4

Related

Numpy.selectand assign new colum in df with condition from values of two column

df = df.assign[test = np.select[df.trs = 'iw' & df.rp == 'yu'],[1,0],'null']
I want if df.trs == iw' and df.rp == 'yu'` than new column should be created should be 0 else 1 only for condotion fullfilling row not every row
I tried no.slect and with condition array. But not getting desired output
You don't need numpy.select, a simple boolean operator is sufficient:
df['test'] = (df['trs'].eq('iw') & df['rp'].eq('yu')).astype(int)
If you really want to use numpy, this would require numpy.where:
df['test'] = np.where(df['trs'].eq('iw') & df['rp'].eq('yu'), 1, 0)

Drop rows from pandas dataframe

I need to drop some rows from a pandas dataframe aa based on a query as follows:
aa.loc[(aa['_merge'] == 'right_only') & (aa['Context Interpretation'] == 'Topsoil')]
How do I drop this selection from the datafram aa?
You can do add '~'
out = aa.loc[~((aa['_merge'] == 'right_only') & (aa['Context Interpretation'] == 'Topsoil'))]
Or
idx = aa.index[(aa['_merge'] == 'right_only') & (aa['Context Interpretation'] == 'Topsoil')]
out = aa.drop(idx)

Dynamic variable assignment based on vector element values in Octave

I need to dynamically assign variable names to each row in a matrix based on the element values of the row so that I can later reference the appropriate row based on the variable rather than an index value.
I am having trouble identifying how to store the variable name along with its associated vector so that the vector can be later pulled up by simply typing in the variable name.
So far, using a simplified example I have:
A = [1 1 0; 1 0 1; 0 1 1];
s = cell(rows(A),1)
i = 1;
for i = 1:rows(A)
if (A(i,1) == 1 & A(i,2) == 1 & A(i,3) == 0) s(i,1) = struct("x1_y1_z0",{A(i,:)})
elseif (A(i,1) == 1 & A(i,2) == 0 & A(i,3) == 1) s(i,1) = struct("x1_y0_z1",{A(i,:)})
elseif (A(i,1) == 0 & A(i,2) == 1 & A(i,3) == 1) s(i,1) = struct("x0_y1_z1",{A(i,:)});
endif
i++
endfor
However, the resulting structue s lists x1_y1_z0 = 1 1 0 in a cell [1,1] and s.x1_y1_z0 does not return [1 1 0] as I would like.
Thanks very much for your guidance towards a working solution.

If Else Statement in vb.Net

I wanted to have this condition like i have b2.text, c3.text, d4.text, e5.text, f6.text, g7.text, h8.text, i9.text and j10.text.
And if they are all equal to zero then statement else continue.
i tried
If (b2.Text = 0 & c3.Text = 0 & d4.Text = 0 & e5.Text = 0 & f6.Text = 0 & g7.Text = 0 & h8.Text = 0 & i9.Text = 0 & j10.Text = 0) Then
a1.Text = 10000000
Else
Msgbox.Show("Cannot sort")
End if
Unfortunately i remembered & function only accept two variable only :P
How can i do it?
Thank you
You don't want & for VB, you want AndAlso. And while it is probably a bit above your level, you might want to look into Linq.
If ({b2.Text, c3.Text, d4.Text, e5.Text, f6.Text, g7.Text, h8.Text, i9.Text, j10.Text}).All(Function(f) f = "0") Then
a1.Text = 10000000
Else
Msgbox.Show("Cannot sort")
End IF
As others have said, you should turn on Option Strict, so that your comparison to an int gets flagged. I made the string "0" above, but you could easily modify it to use length if that is what you actually need.
I don't know what language are you using but I'm pretty sure you can compare more than 2 values The solution may be like this.
if (b2.text == 0 && c3.text == 0 && d4.text == 0 && e5.text== 0 && f6.text == 0 && g7.text == 0 && h8.text == 0 && i9.text == 0 && j10.text == 0) {
// when conditions are true
}
else {
// else code here
}

How to write SQL "not exists" in LINQ

Here I sql query
Select emp.ID,emp.First_Name,w.week_num,w.week_start_dt,w.id,w.week_end_dt
from Employee emp,week_calender w
where not exists (select * from Timesheet where res_id=emp.id and week_cal_id=w.id)
and w.week_start_dt <= '08/06/2014'
order by w.ID
I have written linq query like
var employeesNotEnteredList = (from emps in reslandentity.EMPLOYEE
from week in reslandentity.WEEK_CALENDER
where reslandentity.TIMESHEET.Any(m=>m.RES_ID!=emps.ID && m.WEEK_CAL_ID!=week.ID)
&& week.WEEK_START_DT.Month == month
&& week.WEEK_START_DT.Year == year
&& emps.COMP_ID == Companyid
orderby week.ID
select new TimesheetModel
{
EMP_ID = emps.ID,
EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
RES_TYPE = "EMPLOYEE",
SDate = week.WEEK_START_DT,
EDate = week.WEEK_END_DT,
EMP_STATUS = "NOT_ENTERED"
}).Distinct().ToList();
model.GetTimesheetNotEnteredDetails = employeesNotEnteredList;
But not getting exact results which is getting with sql query
Yes, Got it.
To get results
var employeesNotEnteredList = (from emps in reslandentity.EMPLOYEE
from week in reslandentity.WEEK_CALENDER
where !reslandentity.TIMESHEET.Any(m=>m.RES_ID==emps.ID && m.WEEK_CAL_ID==week.ID)
&& week.WEEK_START_DT.Month == month
&& week.WEEK_START_DT.Year == year
&& emps.COMP_ID == Companyid
orderby week.ID
select new TimesheetModel
{
EMP_ID = emps.ID,
EMPLOYEE_NAME = emps.FIRST_NAME + " " + emps.LAST_NAME,
RES_TYPE = "EMPLOYEE",
SDate = week.WEEK_START_DT,
EDate = week.WEEK_END_DT,
EMP_STATUS = "NOT_ENTERED"
}).Distinct().ToList();