pandas - adding a new column with values from the existing ones -
what's pandas-appropriate way of achieving this? want create column datetime objects 'year','month' , 'day' columns, came code looks way cumbersome:
mylist=[] row in df_orders.iterrows(): #df_orders dataframe mylist.append(dt.datetime(row[1][0],row[1][1],row[1][2])) #-->year, month , day 0th,1st , 2nd columns. myseries=pd.series(mylist,index=df_orders.index) df_orders['mydateformat']=myseries thanks lot help.
try this:
in [1]: df = pd.dataframe(dict(yyyy=[2000, 2000, 2000, 2000], mm=[1, 2, 3, 4], day=[1, 1, 1, 1])) convert integer:
in [2]: df['date'] = df['yyyy'] * 10000 + df['mm'] * 100 + df['day'] convert string, datetime (as pd.to_datetime interpret integer differently):
in [3]: df['date'] = pd.to_datetime(df['date'].apply(str)) in [4]: df out[4]: day mm yyyy date 0 1 1 2000 2000-01-01 00:00:00 1 1 2 2000 2000-02-01 00:00:00 2 1 3 2000 2000-03-01 00:00:00 3 1 4 2000 2000-04-01 00:00:00
Comments
Post a Comment