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
0    1985_1_9                1
1   1985_2_10                1
2   1986_3_11                1
3   1986_4_12                1
4   1987_5_13                1

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