在大熊猫中显示列并将其添加到数据框中

问题描述 投票:0回答:1

我已经创建了一个名为date的数据框,如下所示,但是我无法通过在其他单元格中键入date来调用该数据框。我需要将该数据框称为表,并向其中添加新列。

现在,这就是我所拥有的:

date = data.groupby(pd.to_datetime(data ['Completion Date'],format ='%d。%m。%Y')。dt.month)['Learning Hours']。sum()date.index = pd.to_datetime(date.index,format ='%m')。month_name()。str [:3]date.rename_axis('Month')。reset_index(name ='Learning hours')

O / P:

每月学习时间

Jan 349.5

2月417.0

我需要在此表中添加一个名为Required的新列,所有行中的值均为430,如下所示:

所需的O / P:

需要每月学习时间

Jan 349.5 430

二月417.0 430

python pandas
1个回答
0
投票

用途:

date = data.groupby(pd.to_datetime(data['Completion Date'], format='%d.%m.%Y').dt.month)['Learning Hours'].sum() 
date.index = pd.to_datetime(date.index, format='%m').month_name().str[:3] 
date = date.rename_axis('Month').reset_index(name='Learning hours')
date['Required'] = 430
© www.soinside.com 2019 - 2024. All rights reserved.