Is it possible to use column names with spaces in NamedAgg expressions? - pandas

Is it possible to create names with spaces and special characters when naming an aggregation column using pandas.NamedAgg aggregation function? The typical syntax would be:
pvt = (df.groupby(by=[....])
.agg(value=pd.NamedAgg(column='col', aggfunc='count'))
)
But is there a way to create a column name that is not a valid python variable name (as value in this example), but something like 'my new column name'?
The only solution that I can think of now is to rename value afterwards:
.rename(columns={'value': 'my new column name'})

If your column name does not make a valid Python variable name, you can use dictionary unpacking:
df.groupby(...).agg(**{
'My New Column Name': ('col_name', 'mean'),
'Column 42##$': ('col_name', 'max')
})

Related

Filter centered column SQL Oracle

I try to create a query from an Oracle DB.
that is, SELECT FROM and WHERE.
the column "ORG" is centered and always has 4 letters. I would like to filter that on one specific Item/ value.
I already have WHERE ORG = 'HHAH'
or with SBSTRG (ORG ...:
somehow nothing works.
Does somebody has any idea?
I have values of ' HHAH ' instead of 'HHAH' in the column. There are blanks befor and after the value
You could remove the leading and trailing spaces with the trim() function:
WHERE TRIM(ORG) = 'HHAH'
Using a function on the column value will prevent any index on that column being used (as will like with a leading wildcard); unless you add a function-based index for the trimmed value there isn't much you can do about that.
Do you need the LIKE operator:
WHERE ORG LIKE '%HHAH%'
or
WHERE ORG LIKE '%' || 'HHAH' || '%'
to search for values conatining 'HHAH'?
I would recommend fixing the data:
update t
set org = trim(org);
I see no reason to be storing spaces in the name of an org. If you need spaces for reporting purposes, put them there.

Combine several columns in one with Teradata

I have 10 columns and their values could be either null, or a name of a fruit.
I would like to add another column with all the fruits that every row has. I have used Concat(column1 , column2,..., column10) as name.
Issue : There are no commas coming on the result and if I add the comma before concatenating, we are having them together, the last word is also a comma.
Any ideas?
Thanks!
You can use the standard concatenation (||) in conjunciton with COALESCE function, which returns the value of the first non-null argument.
Example:
select coalesce(column1||',', '')||coalesce(column2||',', '')|| ... ||coalesce(column10||, '');

How to Split String {} with Multiple Values

Let's say that I have a type STRING column 'debugdata'. An example value for a given user looks like this:
{"TITLE_DESCRIPTION":"approve","CATEGORY":"approve"}
However, let's say there can be multiple values for the TITLE_DESCRIPTION
{"TITLE_DESCRIPTION":"No, name does not match,No, summary is not clear","CATEGORY":"Yes"}
How can I split out the "No, name does not match" and "No, summary is not clear" into two columns?
I've tried using JSON_EXTRACT and JSON_ARRAY_GET and other JSON syntax, but I can't quite break this up into two columns. Thanks!
lets say x is your map from your example
let x = {"TITLE_DESCRIPTION":"No, name does not match,No, summary is not clear","CATEGORY":"Yes"}
so all you need to do is this:
let b = (x.TITLE_DESCRIPTION).split(',')
edit: in you example you split the sentences with ',' but have ',' in the string himself so use char escape for ',' or use other char for split the sentences and send it to the split function instead of ','.
What about first use json_extract and then use the String function split?

Find substring in string

Is it possible to check if a specific substring which is in SQL Server column, is contained in a user provided string?
Example :
SELECT * FROM Table WHERE 'random words to check, which are in a string' CONTAINS Column
From my understanding, CONTAINS can't do such kind of search.
EDIT :
I have a fully indexed text and would like to search (by the fastest method) if a string provided by me contains words that are present in a column.
You can use LIKE:
SELECT * FROM YourTable t
WHERE 'random words ....' LIKE '%' + t.column + '%'
Or
SELECT * FROM YourTable t
WHERE t.column LIKE '%random words ....%'
Depends what did you mean, first one select the records that the column has a part of the provided string. The second one is the opposite.
Just use the LIKE syntax together with % around the string you are looking for:
SELECT
*
FROM
table
WHERE
Column LIKE '%some random string%'
This will return all rows in the table table in which the column Column contains the text "some random string".
1) If you want to get data starting with some letter you can use % this operator like this in your where clause
WHERE
Column LIKE "%some random string"
2) If you want to get data contains any letter you can use
WHERE
Column LIKE "%some random string%"
3)if you want to get data ending with some letter you can use
WHERE
Column LIKE "some random string%"

Refering to a column name that is a number

my problem is: I've got a table with several columns which names are numbers (e.g. i've got a column named 1. How can I refer to these columns?
put square brackets round them like this [1], [2]
you can also do this if you have columns named after SQL keywords
You can put square braces or double quotation marks around the column names.
select [1], "2" from YourTable