d=df.key.str.extract(pat=r"\[0-9\]\*\[.\]\[0-9\]\[0-9\]",expand=True)
key value
0 Date 2021-04-05 870.929688 Name: Adj Close,... SBILIFE.NS
1 Date 2021-04-05 1386.009521 Name: Adj Close... SRTRANSFIN.NS
2 Date 2021-04-06 39.354458 Name: Adj Close, ... NACLIND.NS
3 Date 2021-04-06 1397.550781 Name: Adj Close... SHRIRAMCIT.NS
4 Date 2021-04-07 70.805176 Name: Adj Close, ... EDELWEISS.NS
Though the key column is shown above index, its basically the date column(2nd), I want to extract the share price which is there after date in each row and before Name (like 870.929688 in first row) my code is unable to extract data and giving error "pattern contains no capture groups"
please help
d=df4.value.str.extract(pat=r"\[0-9\]\*\[.\]\[0-9\]\[0-9\]",expand=True)
CodePudding user response:
The following regex will capture the stock price in the first and only capture group:
\d -\d -\d \s (.*?)\s*Name
CodePudding user response:
You can use for example
\b(\d (?:\.\d )?)\s Name:
The pattern matches:
\b
A word boundary to prevent a partial word match(
Capture group 1 (for str.extract)\d (?:\.\d )?
Match 1 digits with an optional decimal part
)
Close group 1\s Name:
Match 1 whitespace chars andName:
See a regex101 demo
For example:
import pandas as pd
strings = [
"Date 2021-04-05 870.929688 Name: Adj Close,... SBILIFE.NS",
"Date 2021-04-05 1386.009521 Name: Adj Close... SRTRANSFIN.NS ",
"Date 2021-04-06 39.354458 Name: Adj Close, ... NACLIND.NS ",
"Date 2021-04-06 1397.550781 Name: Adj Close... SHRIRAMCIT.NS ",
"Date 2021-04-07 70.805176 Name: Adj Close, ... EDELWEISS.NS"
]
df = pd.DataFrame(strings, columns=["value"])
df['price'] = df.value.str.extract(pat=r"\b(\d (?:\.\d )?)\s Name:", expand=True)
print(df)
Output
value price
0 Date 2021-04-05 870.929688 Name: Adj Close,...... 870.929688
1 Date 2021-04-05 1386.009521 Name: Adj Close...... 1386.009521
2 Date 2021-04-06 39.354458 Name: Adj Close, ...... 39.354458
3 Date 2021-04-06 1397.550781 Name: Adj Close...... 1397.550781
4 Date 2021-04-07 70.805176 Name: Adj Close, ...... 70.805176