Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String in Pandas
#12
(Apr-07-2017, 12:46 PM)zivoni Wrote: If you need to find string in any colum, you will probably need to iterate over columns and checks each seperately.

(Apr-11-2017, 06:46 AM)yamanda Wrote: How to find in sheet Goal without know position? Example :
Output:
In [2]: df = pd.DataFrame({'col1':['foul', 'goal', 'pass'], 'col2':[1,3,2], 'col3':['goal', 'foul','goal 2'], 'col4':['foul', 'pass', 'foul'    ...: ]}) In [3]: df Out[3]:    col1  col2    col3  col4 0  foul     1    goal  foul 1  goal     3    foul  pass 2  pass     2  goal 2  foul In [4]: for column in df:    ...:     found = df[df[column].astype(str) == 'goal'].index    ...:     if len(found):    ...:         print(column, found.values)    ...:          col1 [1] col3 [0] In [5]: for column in df:    ...:     found = df[df[column] == 2].index    ...:     if len(found):    ...:         print(column, found.values)    ...:          col2 [2]
For string containing some substring you need to use .str.contains(). You can combine multiple conditions in selection, but you need to be careful about column types.
Reply


Messages In This Thread
String in Pandas - by yamanda - Apr-06-2017, 11:41 AM
RE: String in Pandas - by wavic - Apr-06-2017, 12:09 PM
RE: String in Pandas - by buran - Apr-06-2017, 12:10 PM
RE: String in Pandas - by zivoni - Apr-06-2017, 12:53 PM
RE: String in Pandas - by yamanda - Apr-07-2017, 07:27 AM
RE: String in Pandas - by zivoni - Apr-07-2017, 08:03 AM
RE: String in Pandas - by yamanda - Apr-07-2017, 10:41 AM
RE: String in Pandas - by snippsat - Apr-07-2017, 11:30 AM
RE: String in Pandas - by wavic - Apr-07-2017, 11:49 AM
RE: String in Pandas - by zivoni - Apr-07-2017, 12:46 PM
RE: String in Pandas - by yamanda - Apr-11-2017, 06:46 AM
RE: String in Pandas - by zivoni - Apr-11-2017, 10:45 AM
RE: String in Pandas - by yamanda - Apr-12-2017, 07:57 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to search for specific string in Pandas dataframe Coding_Jam 1 2,498 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  Parse XML String in Pandas Dataframe creedX 2 7,031 Dec-09-2019, 07:35 PM
Last Post: creedX
  pandas: can we look for the index of a string paul18fr 2 2,292 Jul-31-2019, 08:25 AM
Last Post: paul18fr
  Simple String to Time within a pandas dataframe Ecniv 1 2,572 Jun-14-2019, 03:25 AM
Last Post: scidam
  Converting string the pandas dataframe chrismc 0 2,392 Jan-24-2019, 11:07 AM
Last Post: chrismc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020