Skip to content Skip to sidebar Skip to footer

A Complex Transformation Of A Data Set In Pandas

I have the following data frame: dictionary = {'Year': [1985, 1985, 1986, 1986, 1987, 1987], 'Wteam' :[1, 2, 3, 4, 5, 6], 'lteam': [ 9, 10, 11, 12, 13, 14] } pdf = pd.DataFrame(di

Solution 1:

You can do the following :

final = pd.DataFrame()
final['team values'] = pdf['Year'].astype('str') + '_' + pdf['Wteam'].astype('str') + '_' + pdf['lteam'].astype('str')
final['predicted_value'] = 1

Solution 2:

One way to do without creating a new dataframe is:

In [15]: pdf['team values'] = pdf.apply(lambda row: str(row['Year'])+'_'+ str(row['Wteam'])+'_'+str(row['lteam']), axis=1)

In [16]: pdf['predicted_value'] = 1

In [17]: pdf.drop(['Wteam','Year','lteam'],axis=1,inplace=True)

In [18]: print pdf.head()
  team values  predicted_value
01985_1_9                111985_2_10                121986_3_11                131986_4_12                141987_5_13                1

Post a Comment for "A Complex Transformation Of A Data Set In Pandas"