Given the following pandas df:
import pandas as pd
df = pd.DataFrame({'1' : ['title1','R','R','R'],
'2' : ["title2", "NR" ,"NR", "NR"],
'3' : ["title3", "R" , "NR", "NR"],
'4' : ["title4", "R", "NR", "R"]})
And a longer list of strings:
List = ['2633', 'title1', '3327', 'title2', '18', 'title3', '5', 'title4', '5835', 'title5', '394', 'title6']
Is there any possibility in python environment to replace the titles in the df with the number before each pair-title in the list of strings.
Expected output:
dfnew = pd.DataFrame({'1' : ['2633','R','R','R'],
'2' : ["3327", "NR" ,"NR", "NR"],
'3' : ["28", "R" , "NR", "NR"],
'4' : ["5", "R", "NR", "R"]})
dfnew
1 2 3 4
0 2633 3327 28 5
1 R NR R R
2 R NR NR NR
3 R NR NR R
I assume that a regex
would do the trick but I do not know how to access the correct numbers from the list.
Thanks for every help in advance!
list
, even though you use capitalL
.