0

this is my original Q

python how do I perform the below operation in dataframe

I want to check how do I start with the output and get the original df back.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
user13412850
  • 509
  • 1
  • 5
  • 16

1 Answers1

0

Use Series.str.extract for numbers and strings to 2 new columns and then reshape by DataFrame.pivot, last some data processing to match input:

df[['Year','col']] = df['Year'].str.extract('(\d+)([A-Z])')
df = (df.pivot('Year','col','Val')
        .rename(columns={'A':'Tval1','B':'Tval2'})
        .reset_index().rename_axis(None, axis=1))
df['Year'] +='A'
print (df)
  Year  Tval1  Tval2
0   1A      1     34
1   2A      9     56
2   3A      8     67
3   4A      1     78
4   5A      6     89
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252