Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How to select a cell that contains certain string and number?

$
0
0

so I have a dataframe that includes strings, ints and floats. To sum up, it looks something like this:

DateCol 1Col 2Col 3Col 3Col 4Col 5Col 6
09/2023181.7017.70 / 22.2214.40109.00NaN60 / NTD
06/2023143.6017.0015.30190.05NaN6 / 11.411
03/20235 / 18.68NC / 17.67NC / 30.02NaNNaN5 / NTDNC / 2

For each row, I'm trying to apply 3 conditionals:

  1. if the cell contains number / number, replace that within same df for the text "Division"
  2. if the cell contains NC / number, replace that within same df for the text "No changes"
  3. if the cell contains number / NTD, replace that within same df for the text "No ToDo's"

Note that everything will be in the same dataframe, that's just changing the text for the conditions that the code must detect. That said, this should be the expected output:

DateCol 1Col 2Col 3Col 3Col 4Col 5Col 6
09/2023181.70Division14.40109.00NaN6No ToDo's
06/2023143.6017.0015.30190.05NaNDivision1
03/2023DivisionNo changesNo changesNaNNaNNo ToDo'sNo changes

My attempt for one of them (condition # 2):

 for k in range(0, df.shape[1]):>                for m,p in enumerate(df[df.columns[(k)]]):                      if (p == ("<NC> / ")):                         df[df.columns[k]][m] = df[df.columns[k]][m].replace(p, 'No changes')

Similarly, I applied the above code for the 'number / NTD' (condition #3) and seems to doing well, however, the condition # 1 has been slightly complicated because it needs to be the first one stated in the code (not in the else part of the if condition) and I don't know how to apply the 'if' condition for any number that's before and after the '/'

Any help on that will be appreciated!


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>