select

Selecting observations by group based on conditions in Python

Selecting observations by group based on conditions in Python Question: Imagine that I have a dataframe that looks like this: import pandas as pd df1 = pd.DataFrame(columns=[‘asset’, ‘eligible’, "date"]) df1[‘asset’] = [‘003’, ‘001’, ‘002’, ‘001’, ‘002’, ‘001’, ‘003’, ‘001’, ‘003’, ‘004’, ‘005’, ‘006’] df1[‘eligible’] = [0, 1, 0, 1, 0, 0, 0, 0, 1, 0, …

Total answers: 1

How to separate everything inside my sqlite database

How to separate everything inside my sqlite database Question: i have a database which has two thing in my database, name of a fruit and how long is it in the freezer. when i use cur1.execute("""SELECT fruit_name, freeze_time, COUNT (*) AS total_fruit FROM fruit_page GROUP BY fruit_name, freeze_days""") rows = cur1.fetchall() rows will actually show …

Total answers: 1

How to launch a conditinated selection of dataframe's row with mixed values

How to launch a conditinated selection of dataframe's row with mixed values Question: I am trying to use the conditioned selection of interested rows/columns into the followng dataset: import pandas as pd already_read = [("Il nome della rosa","Umberto Eco", 1980), ("L’amore che ti meriti","Daria Bignardi", 2014), ("Memorie dal sottsuolo", " Fëdor Dostoevskij", 1864), ("Oblomov", "Ivan …

Total answers: 1

How clear stdin in Python

How clear stdin in Python Question: I would like to know if can be cleared the stdin after some key has been entered. I am doing an input timeout following a response from this forum like this one: while True: web_scrapping() print ("Press ENTER: n") time.sleep(interval) i, o, e = select.select( [sys.stdin], [], [], 10 …

Total answers: 1

How to change multiple columns' types in pyspark?

How to change multiple columns' types in pyspark? Question: I am just studying pyspark. I want to change the column types like this: df1=df.select(df.Date.cast(‘double’),df.Time.cast(‘double’), df.NetValue.cast(‘double’),df.Units.cast(‘double’)) You can see that df is a data frame and I select 4 columns and change all of them to double. Because of using select, all other columns are ignored. …

Total answers: 4

Select one element from a list using python following the normal distribution

Select one element from a list using python following the normal distribution Question: I would like to select one element from a list using python following the normal distribution. I have a list, e.g., alist = [‘an’, ‘am’, ‘apple’, ‘cool’, ‘why’] For example, according to the probability density function (PDF) of normal distribution, the 3rd …

Total answers: 2

Simple SELECT statement on existing table with SQLAlchemy

Simple SELECT statement on existing table with SQLAlchemy Question: Nowhere on the internet does there exist a simple few-line tutorial on a simple SELECT statement for SQLAlchemy 1.0. Assuming I’ve established my database connection using create_engine(), and my database tables already exist, I’d like to know how to execute the following query: select name, age …

Total answers: 5

Select only one index of multiindex DataFrame

Select only one index of multiindex DataFrame Question: I am trying to create a new DataFrame using only one index from a multi-indexed DataFrame. A B C first second bar one 0.895717 0.410835 -1.413681 two 0.805244 0.813850 1.607920 baz one -1.206412 0.132003 1.024180 two 2.565646 -0.827317 0.569605 foo one 1.431256 -0.076467 0.875906 two 1.340309 -1.187678 …

Total answers: 4

How to check if a value is in the list in selection from pandas data frame?

How to check if a value is in the list in selection from pandas data frame? Question: Looks ugly: df_cut = df_new[ ( (df_new[‘l_ext’]==31) | (df_new[‘l_ext’]==22) | (df_new[‘l_ext’]==30) | (df_new[‘l_ext’]==25) | (df_new[‘l_ext’]==64) ) ] Does not work: df_cut = df_new[(df_new[‘l_ext’] in [31, 22, 30, 25, 64])] Is there an elegant and working solution of the …

Total answers: 2

Select 50 items from list at random

Select 50 items from list at random Question: I have a function which reads a list of items from a file. How can I select only 50 items from the list randomly to write to another file? def randomizer(input, output=’random.txt’): query = open(input).read().split() out_file = open(output, ‘w’) random.shuffle(query) for item in query: out_file.write(item + ‘n’) …

Total answers: 5