Pandas 使用现有列中的值添加新列

Pandas 使用现有列中的值添加新列,pandas,Pandas,实现这一目标最合适的方式是什么?我想用“年”、“月”和“日”列中的datetime对象创建一个列,但我想到的只是一些看起来过于繁琐的代码: myList=[] for row in df_orders.iterrows(): #df_orders is the dataframe myList.append(dt.datetime(row[1][0],row[1][1],row[1][2])) #-->year, month and day are the 0th,1st

实现这一目标最合适的方式是什么?我想用“年”、“月”和“日”列中的datetime对象创建一个列,但我想到的只是一些看起来过于繁琐的代码:

myList=[]
for row in df_orders.iterrows():  #df_orders is the dataframe
    myList.append(dt.datetime(row[1][0],row[1][1],row[1][2]))
    #-->year, month and day are the 0th,1st and 2nd columns.
mySeries=pd.Series(myList,index=df_orders.index)
df_orders['myDateFormat']=mySeries
非常感谢您的帮助。

试试这个:

In [1]: df = pd.DataFrame(dict(yyyy=[2000, 2000, 2000, 2000], 
                               mm=[1, 2, 3, 4], day=[1, 1, 1, 1]))
转换为整数:

In [2]: df['date'] = df['yyyy'] * 10000 + df['mm'] * 100 + df['day']
转换为字符串,然后转换为日期时间(如
pd.to_datetime
将以不同方式解释整数):

这也是另一种方式
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