How to create a data frame from another data frame, filter rows and select specific columns all at the same time? [duplicate] - pandas

I know this error message has been discussed on here before but I still cannot figure out how to make this work. I'm trying to do a np.where statement with more than one condition. Below is my code. I am getting the error message "Keyword can't be an expression" and it is highlighting the space after "aggregate['Counter'] > 1.
aggregate['1'] = np.where(np.logical_and(aggregate['Counter'] > 1, aggregate['2'].shift(1) = aggregate['3']), 0, aggregate['2'])

The comparison operator is ==, not =:
...aggregate['2'].shift(1) == aggregate['3']),...
^^ here

You need a double equals sign:
aggregate['1'] = np.where(np.logical_and(aggregate['Counter'] > 1, aggregate['2'].shift(1) == aggregate['3']), 0, aggregate['2']

Related

SSRS Group Sort Using Group Variable

I'm looking to sort group Part using variable Coverage. The variable's expression:
=( IIF(ISNOTHING( SUM(Fields!OnhandQty.Value) ), 0, SUM(Fields!OnhandQty.Value)) +
IIF(ISNOTHING( Fields!WIP.Value ), 0, Fields!WIP.Value)
) / IIF(ISNOTHING( SUM(Fields!RequiredQuantity.Value) ), 0, SUM(Fields!RequiredQuantity.Value) )
I'm able to save report (using Report Builder) with no errors, but I get the error:
The processing of SortExpression for the table ‘TablixCustomer’ cannot be performed. The comparison failed. Please check the data type returned by the SortExpression. (rsProcessingError)
Why?
Your expression SUM(...Value) will fail if .Value is ever null and becomes a String value of #error. So try changing your expressions to this: Note that it checks the value before aggregating the value instead of aggregating before checking for null.
=( SUM(IIF(ISNOTHING(Fields!OnhandQty.Value), 0, Fields!OnhandQty.Value)) +
IIF(ISNOTHING( Fields!WIP.Value ), 0, Fields!WIP.Value)
) / SUM(IIF(ISNOTHING(Fields!RequiredQuantity.Value), 1, Fields!RequiredQuantity.Value))

How to implement slicing in docplex?

I'm transferring some code from OPL to docplex and I'm having troubles with the syntax for slicing. I'm attempting to create a set of constraints with a summation over all values with connection to another decision variable.
I've tried both add_constraints and add_constraint (inside a for loop) but I'm getting an invalid syntax error. I used this format with Google OR so I'd expected this to work.
Here's my OPL code:
forall(<var1,var3> in Index2)
sum(<var1,var2,var3> in Index1)
dev_var[var1,var2,var3] == cec_var2[<var1,var3>];
I tried the following in docplex:
for row2 in df1.itertuples():
solver.add_constraint(solver.sum(dec_var[row.var1,row.var2,row.var3]
for row in df2.itertuples()) == dec_var2[row2.var1,row2.var3]
if row2.var1 = row.var1 and row2.var3 = row.var3)
I'm expecting to create a constraint for each var1,var3 combination that includes all relevant indexes of var1,var2,var3 found in the other index/dataframe. With the "if" statement included, I get a "invalid syntax" error, but without it I get constraints that include a full Cartesian join of the other index without any filtering.
In your if statement, did you try using == (equality operator) instead of = (assignment operator)? I'm not sure that python/pandas like assignments in the conditional expression.
for var1 in index2:
for var3 in index2:
model.add_constraint(model.sum(model.sum(model.sum(dev_var[a,b,c] for a in index1) for b in index1) for c in index1) == cec_var2[var1,var3])
there is something wrong,hope a little help for you

How can I use arrayExists function when the array contains a null value?

I have a nullable array column in my table: Array(Nullable(UInt16)). I want to be able to query this column using arrayExists (or arrayAll) to check if it contains a value above a certain threshold but I'm getting an exception when the array contains a null value:
Exception: Expression for function arrayExists must return UInt8, found Nullable(UInt8)
My query is below where distance is the array column:
SELECT * from TracabEvents_ArrayTest
where arrayExists(x -> x > 9, distance);
I've tried updating the comparison in the lambda to "(isNotNull(x) and x > 9)" but I'm still getting the error. Is there any way of handling nulls in these expressions or are they not supported yet?
Add a condition to filter rows with empty list using notEmpty and assumeNotNull for x in arrayExists.
SELECT * FROM TracabEvents_ArrayTest WHERE notEmpty(distance) AND arrayExists(x -> assumeNotNull(x) > 9, distance)

R - Using sqldf to query multiple values from one column in dataframe

I'm pretty new to R and trying to use the SQLDF package to query a dataset. I have constructed the following query, which works perfectly and displays the correct data:
sqldf("select AreaName, TimePeriod, Value from df2 where Indicator == 'Obese children (Year 6)' AND AreaName == 'Barking and Dagenham'",
row.names = TRUE)
But I would like to pull the data for 'Richmond upon Thames' as well as Barking and Dagenham. I have tried this:
AND AreaName == 'Barking and Dagenham', 'Richmond upon Thames'
Which gives me the following error:
Error in sqliteSendQuery(con, statement, bind.data) : error in statement: near ",": syntax error
And I have also tried:
AND AreaName == 'Barking and Dagenham' AND AreaName == 'Richmond upon Thames'
Which creates the new dataframe as expected but when I view it, it is empty. I know it is not an issue with the name 'Richmond upon Thames' as I have entered this into the first statement by itself instead of 'Barking and Dagenham' and it works perfectly.
Could anybody help me with what the correct structure should be?
Many thanks

Appending an numeric value at the and of String

i am trying to insert multiple insert in table. For that i have created an int column table and inserting using for loop function but i am not able to write proper code which i want to do.
i Need some thing like that
for(i=0;i<1800;i++)
{
retcode = SQLPrepare(hstmt,(SQLCHAR *)"insert into dbo.vivtest values(i)",SQL_NTS);
if (retcode != SQL_SUCCESS)
{
printf("Error in SQLPrepare - insert\n");
odbc_Error(henv,hdbc,hstmt);
getch();
}
else
printf("Successfull execution of %d th Prepare\n",i);
It's giving me error every time.
The following is a useful link about SQLPrepare() and how to bind parameters. Binding parameters is a safe way of inserting variable contents into your SQL strings and also a prepared statement made in this way is an efficient way of executing a statement multiple times.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms716365(v=vs.85).aspx
It gives an example of binding parameters to an SQL query. The give the following example:
SQLPrepare(hstmt, "UPDATE Parts SET Price = ? WHERE PartID = ?", SQL_NTS);
In the SQL string you can see a few ? (question marks). These are like placeholders in the SQL string which you can then "bind parameters to" (i.e., substitute in a variable contents in place of).
To continue the MSDN example...
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_REAL, 7, 0,
&Price, 0, &PriceInd);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 10, 0,
&PartID, 0, &PartIDInd);
The first statement replaces the first question mark with a floating point value from the variable Price and the second bind replaces the second question mark with an integer from the PartID variable.
Your prepare statement should probably looks something like...
SQLINTEGER iInd;
SQLUINTEGER i;
...
...
retcode = SQLPrepare(hstmt,(SQLCHAR *)"insert into dbo.vivtest values(?)",SQL_NTS);
...
...
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_ULONG, SQL_INTEGER, 10, 0,
&i, 0, &iInd);
You should use either SQL Parameters, or sprintf().
Preferably the former.