Using conditional to generate new column in pandas dataframe

You can define a function which returns your different states “Full”, “Partial”, “Empty”, etc and then use df.apply to apply the function to each row. Note that you have to pass the keyword argument axis=1 to ensure that it applies the function to rows. import pandas as pd def alert(row): if row[‘used’] == 1.0: return … Read more

Use a calculated column in a where clause

Using Derived Columns in a predicate You’ll need to wrap the inner query in a derived table or CTE in order to be able to use derived columns in the WHERE clause (Also, note SUM() is specified just once, using the results of the multiplication): SELECT x.Code, x.AccountNumber, x.Sales FROM ( SELECT p.Code, c.AccountNumber, SUM(p.UnitPrice … Read more

Sql Server deterministic user-defined function

You just need to create it with schemabinding. SQL Server will then verify whether or not it meets the criteria to be considered as deterministic (which it does as it doesn’t access any external tables or use non deterministic functions such as getdate()). You can verify that it worked with SELECT OBJECTPROPERTY(OBJECT_ID(‘[dbo].[FullNameLastFirst]’), ‘IsDeterministic’) Adding the … Read more

Create new columns and fill with calculated values from same dataframe

You can do this easily manually for each column like this: df[‘A_perc’] = df[‘A’]/df[‘sum’] If you want to do this in one step for all columns, you can use the div method (http://pandas.pydata.org/pandas-docs/stable/basics.html#matching-broadcasting-behavior): ds.div(ds[‘sum’], axis=0) And if you want this in one step added to the same dataframe: >>> ds.join(ds.div(ds[‘sum’], axis=0), rsuffix=’_perc’) A B C … Read more

tech