有没有一种方法可以添加共同共享另一列的列?

时间:2019-03-31 20:11:44

标签: python pandas

当“ start_date”和“ meal_type”相同时,我试图添加“ calorie”列以生成matplotlib图。我能够绘制'calorie'和'start_date',但是我不知道如何添加列并能够绘制它们。这是我在“开始日期”上排序的数据示例:
使用; (分号)作为分隔符

amount;meal_type;start_time;name;calorie  
2;100001;2/19/19 10:11;Black Label Bacon Original(Hormel);180  
1;100001;2/19/19 10:11;Medium Cheddar Cheese(Kraft);120  
2;100001;2/19/19 10:11;Egg;148  
1;100004;2/19/19 13:37;NUT-rition Heart Healthy Mix(Planters);190  
1;100002;2/19/19 16:35;Mayo Real Mayonnaise(Kraft);90  
0.5;100002;2/19/19 16:35;Celery;3  
1;100002;2/19/19 16:35;Classic Cole Slaw Mix(Dole);20  
1;100002;2/19/19 16:35;Chunk Light Tuna in Water (Pouch)(StarKist Foods);70  
0.5;100002;2/19/19 16:35;Baby Spinach;3.5  
1;100005;2/19/19 20:13;String Cheese(Polly-O);80  
0.5;100005;2/19/19 20:13;2% Mexican Cheddar Jack(Kraft);40  
1;100005;2/19/19 20:13;Magic Pop(Kim's);15  
1;100005;2/19/19 20:13;Pepperoni Stix(Hormel);110  
1;100003;2/19/19 20:24;Parmesan Cheese (Grated);22  
1;100003;2/19/19 20:24;Skinless Chicken Breast;130  
1;100003;2/19/19 20:24;Pork Cracklins(Original Mac's);80  
0.4;100003;2/19/19 20:24;Whole Milk Mozzarella Cheese(Polly-O);32  
0.5;100003;2/19/19 20:24;100% Natural Tomato Sauce(Hunt's);10  
2;100001;2/20/19 9:15;Egg;148  
2;100001;2/20/19 9:15;Black Label Bacon Original(Hormel);180  
1;100001;2/20/19 9:15;Medium Cheddar Cheese(Kraft);120  
1;100004;2/20/19 15:00;NUT-rition Heart Healthy Mix(Planters);190  
1;100002;2/20/19 16:22;100% Natural Tomato Sauce(Hunt's);20  
1;100002;2/20/19 16:22;Pork Cracklins(Original Mac's);80  
1;100002;2/20/19 16:22;Skinless Chicken Breast;130  
1;100002;2/20/19 16:22;Parmesan Cheese (Grated);22  
1;100002;2/20/19 16:22;Whole Milk Mozzarella Cheese(Polly-O);80  
1;100005;2/20/19 19:36;String Cheese(Polly-O);80  
1;100005;2/20/19 19:36;Pepperoni Stix(Hormel);110  
0.5;100003;2/20/19 23:30;Almond Meal Flour(Bob's Red Mill);80  
1;100003;2/20/19 23:30;Zucchini Summer Squash (Without Salt, Drained, Cooked, Boiled);29  
3.6;100003;2/20/19 23:30;Baked or Broiled Cod;126  
0.5;100003;2/20/19 23:30;Basil Pesto(Barilla);110  

我的目的是确保'start_time'和'meal_type'相同,并在一天中将卡路里作为新列添加为'total_calories',并显示以下结果:

amount;meal_type;start_time;name;calorie;comment;total_calories  
2;100001;2/19/19 10:11;Black Label Bacon Original(Hormel);180;;  
1;100001;2/19/19 10:11;Medium Cheddar Cheese(Kraft);120;;  
2;100001;2/19/19 10:11;Egg;148;;776  
1;100004;2/19/19 13:37;NUT-rition Heart Healthy Mix(Planters);190;;190  
1;100002;2/19/19 16:35;Mayo Real Mayonnaise(Kraft);90;;  
0.5;100002;2/19/19 16:35;Celery;3;;  
1;100002;2/19/19 16:35;Classic Cole Slaw Mix(Dole);20;;  
1;100002;2/19/19 16:35;Chunk Light Tuna in Water (Pouch)(StarKist Foods);70;;  
0.5;100002;2/19/19 16:35;Baby Spinach;3.5;183.25;  
1;100005;2/19/19 20:13;String Cheese(Polly-O);80;;  
0.5;100005;2/19/19 20:13;2% Mexican Cheddar Jack(Kraft);40;;  
1;100005;2/19/19 20:13;Magic Pop(Kim's);15;;  
1;100005;2/19/19 20:13;Pepperoni Stix(Hormel);110;;225  
1;100003;2/19/19 20:24;Parmesan Cheese (Grated);22;;  
1;100003;2/19/19 20:24;Skinless Chicken Breast;130;;  
1;100003;2/19/19 20:24;Pork Cracklins(Original Mac's);80;;  
0.4;100003;2/19/19 20:24;Whole Milk Mozzarella Cheese(Polly-O);32;;  
0.5;100003;2/19/19 20:24;100% Natural Tomato Sauce(Hunt's);10;;249.8  
2;100001;2/20/19 9:15;Egg;148;;  
2;100001;2/20/19 9:15;Black Label Bacon Original(Hormel);180;;  
1;100001;2/20/19 9:15;Medium Cheddar Cheese(Kraft);120;;776  
1;100004;2/20/19 15:00;NUT-rition Heart Healthy Mix(Planters);190;;190  
1;100002;2/20/19 16:22;100% Natural Tomato Sauce(Hunt's);20;;  
1;100002;2/20/19 16:22;Pork Cracklins(Original Mac's);80;;  
1;100002;2/20/19 16:22;Skinless Chicken Breast;130;;  
1;100002;2/20/19 16:22;Parmesan Cheese (Grated);22;;  
1;100002;2/20/19 16:22;Whole Milk Mozzarella Cheese(Polly-O);80;332;  
1;100005;2/20/19 19:36;String Cheese(Polly-O);80;;  
1;100005;2/20/19 19:36;Pepperoni Stix(Hormel);110;;190  
0.5;100003;2/20/19 23:30;Almond Meal Flour(Bob's Red Mill);80;;  
1;100003;2/20/19 23:30;Zucchini Summer Squash (Without Salt, Drained, Cooked, Boiled);29;;  
3.6;100003;2/20/19 23:30;Baked or Broiled Cod;126;;  
0.5;100003;2/20/19 23:30;Basil Pesto(Barilla);110;;577.6  

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么您想对同一Sub SendToInvoiceList2() Dim ws As Worksheet: Set ws = Worksheets("Invoice") Dim wsData As Worksheet: Set wsData = Worksheets("Invoice List 2") 'declare and set the worksheets, amend as required Dim i As Long, dataRows As Long 'TRANSFER data to Invoice List 2 dataRows = ws.Range("B14").Columns(1).SpecialCells(xlCellTypeConstants, 1).Count 'count the number of Invoice lines with data (non-empty) 'I'm not sure what should go in the ws.Range and the wsData.Range below. ws.Range("B1").Copy wsData.Range("F" & Rows.Count).End(xlUp).Offset(1, 0) 'copy invoice lines to Invoice Data For i = 1 To dataRows 'loop from 1 to however many lines your named range "Invoice" has ws.Range("E4").Copy wsData.Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues ws.Range("D6").Copy wsData.Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues ws.Range("B14").Copy wsData.Range("C" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues ws.Range("D14").Copy wsData.Range("D" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues ws.Range("E14").Copy wsData.Range("E" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues Next i 上的每个meal_type进行求和。

我们可以执行以下操作:

date