Get Value from List to Show in DataFrame Column - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Get Value from List to Show in DataFrame Column (/thread-27592.html) |
Get Value from List to Show in DataFrame Column - ahmedwaqas92 - Jun-12-2020 I have a dataframe with a little more than 15000 rows. I also have three lists (c_iddf, p_iddf, cr_iddf) that I converted from a .csv file, so all the lists have equal number of elements. The dataframe df looks something like this , Login Caller ID Called number Call Start Call end Duration (s) block Effective Duration (s) blocks 0 6072132500 6072132500 0320585339 01/05/2020 16:25 01/05/2020 16:25 19 60 60 1.0 1 6072132500 6072132500 100 01/05/2020 21:04 01/05/2020 21:05 16 60 60 1.0 2 6072132500 6072132500 1300883000 01/05/2020 21:08 01/05/2020 21:08 9 60 60 1.0 3 6072132500 6072132500 15454 01/05/2020 21:13 01/05/2020 21:14 4 60 60 1.0 4 6072132500 6072132500 1300883000 01/05/2020 21:14 01/05/2020 21:14 16 60 60 1.0I want to check if the column 'Called number' has any values from p_iddf. If it has, then record the index of the element in the list p_iddf that matches the Called number value, and use that to get elements (with the same index number) from c_iddf and cr_iddf. I don't know if my logic is correct or not and would like suggestions on how I can do this some other way but if I add: df['Check'] = df['Called number'].str.startswith(tuple(p_iddf))It does give me the correct boolean result (another column called Check with True and False)however I don't know how to get index numbers of p_iddf that matches with 'Called number' & the corresponding values from c_iddf and cr_iddf as per the index. RE: Get Value from List to Show in DataFrame Column - ahmedwaqas92 - Jun-22-2020 I have successfully solved this set by introducing a function which searches stings in the df using a list, that initially joins all the elements I would want to pass through the data frame and then returns the value on a new column of the element that matched in the df. The code is as below: iddf = pd.read_csv('idd.csv', names=['country', 'prefix', 'rate', '1']) iddf = iddf.drop(['1'], axis=1) iddf = iddf.astype(str) iddf['prefix'] = '00' + iddf['prefix'] prefix = iddf['prefix'].values.tolist() #Developing Pattern for IDD Regex Search pattern = '|'.join(prefix) def pattern_searcher(search_str:str, search_list:str): search_obj = re.match(search_list, search_str) if search_obj : return_str = search_str[search_obj.start(): search_obj.end()] else: return_str = 'NaN' return return_str df['prefix'] = df['Called number'].apply(lambda x: pattern_searcher(search_str=x, search_list=pattern))The above code returns a new column called prefix which first matches prefix to the column 'Called number' and then returns the prefix which was matched. Using this prefix I could then map a dictionary and get more values if I want. |