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.
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.
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