Can we do a switch case with an in clause [duplicate] - python

This question already has answers here:
imploding a list for use in a python MySQLDB IN clause
(8 answers)
Closed 2 days ago.
Can we do use an in clause with case
sql_query=f"""SELECT * FROM table1
where column1 in ('{list_of_values}')
order by CASE
WHEN column2 like'a%' THEN 1
WHEN column2 like'b%' THEN 2
WHEN column2 like'c%' THEN 3
ELSE 99 END;
"""
I am not getting any value in return but when I try
sql_query=f"""SELECT * FROM table1
where column1 = '{value1}'
order by CASE
WHEN column2 like'a%' THEN 1
WHEN column2 like'b%' THEN 2
WHEN column2 like'c%' THEN 3
ELSE 99 END;
"""
I get a value in return. What am I doing wrong in the first query. Thanks.

where column1 in ('{list_of_values}')
your '{list_of_values}' isn't going to work, it needs to be each individual item comma separated and within individual quotes e.g. where column1 in ('a','b','c','etc')

Related

Select the count of columns with values with specific name for the columns

My table(car_mileage) structure is like this.
No of cars column (car_Number) will be dynamically populate and will vary depending on different persons.
I need the sql query which gives the count of car_Number columns whose values are more than 0 for a particular name.
example for Joseph - count is 10
for Jose - count is 8
Please Help
--I think it can do in multiple solution. one of this is
-- SQL Server statement
SELECT name,age,County,
CASE WHEN car_1>0 then 1 else 0 END + CASE WHEN car_2>0 THEN 1 ELSE 0 END
+ ... + CASE WHEN car_n>0 THEN 1 ELSE 0 END AS ColumnsCountIsMoreThenZero
FROM MyTable

How to filter pandas dataframe based on length of a list in a column? [duplicate]

This question already has answers here:
How to filter a pandas dataframe based on the length of a entry
(2 answers)
Closed 1 year ago.
I have a pandas DataFrame like this:
id subjects
1 [math, history]
2 [English, Dutch, Physics]
3 [Music]
How to filter this dataframe based on the length of the column subjects?
So for example, if I only want to have rows where len(subjects) >= 2?
I tried using
df[len(df["subjects"]) >= 2]
But this gives
KeyError: True
Also, using loc does not help, that gives me the same error.
Thanks in advance!
Use the string accessor to work with lists:
df[df['subjects'].str.len() >= 2]
Output:
id subjects
0 1 [math, history]
1 2 [English, Dutch, Physics]

extract semicolon separated value from pandas df column [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I need to extract a specific value from pandas df column. The data looks like this:
row my_column
1 artid=delish.recipe.45064;artid=delish_recipe_45064;avb=83.3;role=4;data=list;prf=i
2 ab=px_d_1200;ab=2;ab=t_d_o_1000;artid=delish.recipe.23;artid=delish;role=1;pdf=true
3 dat=_o_1000;artid=delish.recipe.23;ar;role=56;passing=true;points001
The data is not consistent, but separated by a comma and I need to extract role=x.
I separated the data by a semicolon. And can loop trough the values to fetch the roles, but was wondering if there is a more elegant way to solve it.
Desired output:
row my_column
1 role=4
2 role=1
3 role=56
Thank you.
You can use str.extract and pass the required pattern within parentheses.
df['my_column'] = df['my_column'].str.extract('(role=\d+)')
row my_column
0 1 role=4
1 2 role=1
2 3 role=56
This should work:
def get_role(x):
l=x.split(sep=';')
t=[i for i in l if i[:4]=='role')][0]
return t
df['my_column']=[i for i in map(lambda y: get_role(y), df['my_column'])]

Pandas: updating values based on value from run time [duplicate]

This question already has answers here:
Replace Column in Data Frame from Lookup of other Data Frame
(2 answers)
Closed 4 years ago.
I have a dataframe like this:
df1:
Steam feat
1 some_value
2 some_value
3 some_value
4 some_value
I have to update the value in "feat" based on certain condition. For example,
i have to update the feat as "88" when the steam is "2"
The output should look like this:
final output:
Steam feat
1 some_value
2 88
3 some_value
4 some_value
The issue i have here is that, i have to pass the values "2" and "88" in run time values taken from a different table called df2.
df2:
cola colb
2 88
To achieve this, I tried to apply the below code:
df1.loc[df1["Steam"] = df2.cola.values, 'feat'] = df2.colb.values
However i am getting a "invalid syntax" error
the values of df2.cola.values will look like this
array(['2'], dtype=object)
Am I doing anything wrong here? Please advise.
You need to align indices and map your data. This is one way, which should be efficient if you expect a mapping to exist.
df1['feat'] = df1['Steam'].map(df2.set_index('cola')['colb']).fillna(df1['feat'])

Get pandas to print the complete string [duplicate]

This question already has answers here:
Pandas to_html() truncates string contents
(2 answers)
Closed 5 years ago.
I have a pandas dataframe with two columns - one for id and other for the corresponding title. I am subsetting the dataframe for a few project ids and displaying the resulting dataframe. On doing so, the project id gets displayed fine but the corresponding title gets truncated and end with ... after a few characters, How do I get pandas to display the full text in the title column?
You can use display.max_colwidth:
df = pd.DataFrame(np.array([[1,'aaa'],
[2, 'long string long string long string long string long string']]), columns=['id','title'])
print (df)
id title
0 1 aaa
1 2 long string long string long string long strin...
#temporaly display long text
with pd.option_context('display.max_colwidth', 100):
print (df)
id title
0 1 aaa
1 2 long string long string long string long string long string
Documentation

Categories