Python Forum
Get Value from List to Show in DataFrame Column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Value from List to Show in DataFrame Column
#1
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.0
I 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.
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  renaming a column without a name in a dataframe Carbonpony 2 3,810 Jan-23-2025, 08:20 AM
Last Post: Carbonpony
  Adding PD DataFrame column bsben 2 2,009 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 2,170 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Difference one column in a dataframe Scott 0 1,577 Feb-10-2023, 08:41 AM
Last Post: Scott
  splitting a Dataframe Column in two parts nafshar 2 2,522 Jan-30-2023, 01:19 PM
Last Post: nafshar
  PIL Image im.show() no show! Pedroski55 2 5,575 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 2,563 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  renaming the 0 column in a dataframe Led_Zeppelin 5 11,234 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  function returns dataframe as list harum 2 3,980 Aug-13-2022, 08:27 PM
Last Post: rob101
  Copy a column from one dataframe to another dataframe Led_Zeppelin 17 26,184 Jul-08-2022, 08:40 PM
Last Post: deanhystad

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.