How to count the occurrences of a column's value in a column of lists?

Consider the following dataframe: column_of_lists scalar_col 0 [100, 200, 300] 100 1 [100, 200, 200] 200 2 [300, 500] 300 3 [100, 100] 200 The desired output would be a Series, representing how many times the scalar value of scalar_col appears inside the list column. So, in our case: 1 # 100 appears once in… Read More How to count the occurrences of a column's value in a column of lists?

Convert pandas series strings to numbers

`Following series, contains result as string of lists with values either PASS or FAIL. Input:- result "[‘PASS’,’FAIL’]" "[‘PASS’,’FAIL’,’PASS’,’FAIL’]" "[‘FAIL’,’FAIL’]" Output: result 1 1 0 If any row has at-least one PASS as value then return 1 else return 0 Input:- result "[‘PASS’,’FAIL’]" "[‘PASS’,’FAIL’,’PASS’,’FAIL’]" "[‘FAIL’,’FAIL’]" >Solution : If there are lists use in statement: df[‘result’] =… Read More Convert pandas series strings to numbers

multiple series appended in a list using df.iterrows. How do I convert it into a dataframe?

I would like to know how to covert multiple series objects in list into a dataframe. import pandas as pd data = { "firstname": ["Sally", "Mary", "John"], "age": [50, 40, 30], "lastname" : ["a1", "b1", "c1"], } df = pd.DataFrame(data) tmp_list = list() for idx, row in df.iterrows(): tmp_list.append(row) My tmp_list looks like this. [firstname… Read More multiple series appended in a list using df.iterrows. How do I convert it into a dataframe?

Changing the format of a column of data in a Pandas Series

I would like to change the format of the returned dates from this: listOfDates = df2[‘TradeDate’].drop_duplicates().reset_index(drop=True) print(listOfDates) 0 2022-02-02 1 2022-02-08 2 2022-05-01 3 2022-05-06 4 2022-06-05 5 2022-06-17 6 2022-07-30 7 2022-08-03 8 2022-10-10 9 2022-11-18 Name: TradeDate, dtype: datetime64[ns] to this: listOfDates = df2[‘TradeDate’].drop_duplicates().reset_index(drop=True) print(listOfDates) 0 20220202 1 20220208 2 20220501 3 20220506… Read More Changing the format of a column of data in a Pandas Series

How to insert character ('-") every time my string changes from text to number and vice versa?

This is an example of a bigger dataframe. Imagine I have a dataframe like this: import pandas as pd df = pd.DataFrame({"ID":["4SSS50FX","2TT1897FA"], "VALUE":[13, 56]}) df Out[2]: ID VALUE 0 4SSS50FX 13 1 2TT1897FA 56 I would like to insert "-" in the strings from df["ID"] everytime it changes from number to text and from text… Read More How to insert character ('-") every time my string changes from text to number and vice versa?

Set value for all columns based on series

I am trying to set all values of a row to the same value base on another dataframe (or series derived from a dataframe). Simple dfs: df=pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=[‘a’,’b’,’c’]) df2=pd.DataFrame([[‘const’,10,’other’],[‘const’,20,’other’],[‘var’,30,’other’],[‘var’,40,’other’]],columns=[‘type’,’val’,’z’]) df a b c 0 1 2 3 1 4 5 6 2 7 8 9 df2 type val z 0 const 10 other 1 const 20… Read More Set value for all columns based on series

Python pandas series – how can I print only the value without the other information

I have a Python series that gives me the following: df_1974.loc[[df_1974["Close*"].idxmin()]][‘date_final’] type(df_1974.loc[[df_1974["Close*"].idxmin()]][‘date_final’]) df_1974.loc[[df_1974["Close*"].idxmin()]][‘date_final’] Out[8]: 12099 1974-10-03 Name: date_final, dtype: datetime64[ns] type(df_1974.loc[[df_1974["Close*"].idxmin()]][‘date_final’]) Out[9]: pandas.core.series.Series How can I print only the value 1974-10-03 without the other information? >Solution : You can convert this to_numpy() or to_list() or values and print the 0th element. e.g., >>> df_1974.loc[[df_1974["Close*"].idxmin()]][‘date_final’].astype("str").to_list()[0] >>>… Read More Python pandas series – how can I print only the value without the other information