How to extract number from string in pandas? [duplicate] - python-3.x

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
RegEx - Match Numbers of Variable Length
(4 answers)
Closed 4 months ago.
My case is extract number between text (ex: FL-number-$) from string in File names column to Check column. Example:
2022-06-09-FR-Echecks.pdf > Return ''
2022-06-09-FR-FL-3-$797.pdf > Return 3
2022-06-09-FR-TX-20-$35149.91.pdf > Return 20
My case as below
This code I used:
dt_test['File_names_page'] = dt_test['File names'].str.extract('\-([0-99])-\$')
It only return one digit number as below:
So how to extract all number (all digit) in my case?
Tks for all attention!

Your regex pattern is slightly off. Just use \d+ to match any integer number:
dt_test["File_names_page"] = dt_test["File names"].str.extract(r'-(\d+)-\$')

You can't use a 0-99 range, you should use \d{1,2} for one or two digits:
dt_test['File_names_page'] = dt_test['File names'].str.extract(r'-(\d{1,2})-\$')
Or for any number of digits (at least 1) \d+:
dt_test['File_names_page'] = dt_test['File names'].str.extract(r'-(\d+)-\$')
NB. - doesn't require an escape
Example:
File names File_names_page
0 ABC-12-$456 12

Related

Extract 9 last number from a number of 14 digit

One of my Excel column of my board have to store numbers of 9 digits.
I'm looking for a solution to keep only the 9 last digits of any bigger number past in this specific column. It's only entire number.
Also if after formatting the number it appear that the number starts with 0 the 0 have to be kept. Is there another solution than adding an '0 at first ?
Here is what I already done : (i is the row number / Range01 is Range("A14:O400"))
If Len(Range01.Cells(i,5).value) = 9 Then
Range01.Cells(i,5).Interior.color = vbGreen
ElseIf Len(Range01.Cells(i,5).value) = 8 Then
Range01.Cells(i,5).value = "'0" & Range01.Cells(i,5).value
ElseIf Len(Range01.Cells(i,5).value) > 9 Then
????
Else
Range01.Cells(i,5).Interior.color = vbRed
End If
Thanks for the help.
The simplest way to get the last nine numbers of an integer is:
=MOD(A1,1000000000)
(For your information, that's one billion, a one with nine zeroes.)
If you're interested in showing a number with leading zeroes, you can alter the cell formatting as follows: (the format simply contains nine zeroes)
If you're interested in keeping the zeroes, you might need to use your number as a string, and precede it with a good number of repeated zeroes, something like:
=REPT("0",9-LEN(F8))&F8
Take the length of your number (which gets automatically converted into a string)
Subtract that from 9 (so you know how many zeroes you need)
Create a string, consisting of that number of zeroes
Add your number behind it, using basic concatenation.
You can simply use the math operator of modulus. If you want the last 9 digit you can write:
n % 10000000000
Where n is the number in the column.
In VBA:
MOD(n,1000000000)

Getting the number in the ones place python3 [duplicate]

This question already has answers here:
How to check last digit of number
(10 answers)
Closed 4 years ago.
I was wondering if there was any way to get the ones place of a integer. For example, if I had the number 41, would there be any easy way to get 1 from that? Thanks!
Take modulo by 10
a = float(input('Enter a number : '))
print( a % 10)
Modulo operator return the remainder of number so any number modulo with 10 will return its ones number for more https://docs.python.org/3/reference/expressions.html

How to re-arrange string so that same characters are not next to each other? [duplicate]

This question already has answers here:
Lexicographic minimum permutation such that all adjacent letters are distinct
(6 answers)
re-arrange items into an array with no similar items next to each other
(3 answers)
Closed 8 years ago.
How to re-arrange string so that same characters are not next to each other and if there are many alternative sorting options we'll choose the one which is alphabetically sorted?
i.e.
AAABBBB -> BABABAB
AAABBB -> ABABAB
BCDDEEEF -> BCEDEDEF
BACHH -> ABHCH
Pseudo code or something would be useful.
A naive solution:
Find all permutations of the string
Find all that don't have repeating characters
Find the first alphabetically

python3: how do i format 2 as 02 [duplicate]

This question already has answers here:
Display number with leading zeros [duplicate]
(19 answers)
Closed 8 years ago.
Apologies if this is a repeat question but,
what formatting commands do i need to use if I want a single digit number to be displayed with a zero in front?
i.e. the number '2' would be displayed as '02'.
[But, I do not want any value above 10 to have extra zeros in front]
cheers
You can use this syntax:
>>> "{:0>2}".format(2)
'02'
>>> "{:0>2}".format(98)
'98'
>>> "{:x>4}".format(2)
'xxx2'
More info: Common string operations
Try:
if 0 < num < 10:
return "0" + str(single_digit)

formatting character arrays [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Counting values by day/hour with timeseries in MATLAB
This is an elementary question, but I cannot find it:
I have a 3000x25 character array:
2000-01-01T00:01:01+00:00
2000-01-01T00:01:02+00:00
2000-01-01T00:01:03+00:00
2000-01-01T00:01:04+00:00
These are obviously times. I want to reformat the array to be a 3000x1 array. How can I redefine each row to be one entry in an array?
(Again, this is simple, I'm sorry)
Other than converting to serial date numbers as other have shown, I think you simply wanted to convert to cell array of strings:
A = cellstr(c)
where c is the 3000x25 matrix of characters.
You need to specify a format for the array and feed it to datenum, like this:
>> d = datenum(c,'YYYY-MM-DDTHH:mm:ss')
d =
1.0e+005 *
7.3487
7.3487
7.3487
7.3487
The times are now stored as datenums, i.e. as floating point numbers representing the number of days elapsed since the start of the Matlab epoch. If you want to convert these to numbers representing the fraction of the day elapsed, you can do
>> t = d - fix(d);
and if you want the number of seconds since midnight, you can do
>> t = 86400 * (d - fix(d));
t =
61.0000
62.0000
63.0000
64.0000

Resources