i Have a dataframe DF , and column name DETAILS, i want the value which are in double quotes (""), will make new column VALUE
DF=
DETAILS
Username as "JOHN"
verifying the name
click UserBox
Password as "345678"
Click login
click checkbox
Phonenumber as "34512345"
Checking data
I want my data frame look like this
DF=
DETAILS VALUE
Username as "JOHN" "JOHN"
verifying the name NA
click UserBox NA
Password as "345678" "3345678"
Click login NA
click checkbox NA
Phonenumber as "34512345" "34512345"
Checking data NA
CodePudding user response:
The str
accessor has an extract
method for that kind of use case:
DF['VALUE'] = DF['DETAILS'].str.extract('(".*?")')
With your data, it gives as expected:
DETAILS VALUE
0 Username as "JOHN" "JOHN"
1 verifying the name NaN
2 click UserBox NaN
3 Password as "345678" "345678"
4 Click login NaN
5 click checkbox NaN
6 Phonenumber as "34512345" "34512345"
7 Checking data NaN
CodePudding user response:
Here you go:
df['VALUE'] = df.apply(
lambda x: '"' x['DETAILS'].split('"')[1] '"'
if len(x['DETAILS'].split('"')) > 1
else 'NA',
1)
This will output:
DETAILS VALUE
0 Username as "JOHN" "JOHN"
1 verifying the name NA
2 click UserBox NA
3 Password as "345678" "345678"
4 Click login NA
5 click checkbox NA
6 Phonenumber as "34512345" "34512345"
7 Checking data NA
with the column VALUES containing your values where present, else NA.