熊猫:根据内容将列表分成不同的列

时间:2017-09-01 15:45:17

标签: python pandas

我有以下列表:

brand_size = ['Brand:Ashfield Farm; Size / Weight:375g',
              'Size / Weight:170g',
              '',
              'Brand:Lacura; Size / Weight:750 ml',
              '',
              '',
              "Brand:Nature's Glen",
              '',
              '',
              '',
              'Brand:Ashfield Farm; Size / Weight:450g',
              'Brand:Specially Selected',
              'Brand:Specially Selected',
              '',
              '',
              '',
              '',
              '']

我想要的是:一个数据框,列出“品牌”和“尺寸/重量”(如果有),如果没有则留空。 E.g。

pd.DataFrame({'Brand':['Ashfield Farm', '', '', 'Lacura', '', '', "Nature's 
                       Glen", '', '', '', 'Ashfield Farm', 'Specially 
                       Selected', 'Specially Selected', '', '', '', '', ''],
              'Size / Weight':['375g', '170g', '', '750ml', '', '', '', 
                               '','', '', '450g', '', '', '', '', '', '', '']})

                 Brand   Size / Weight
0        Ashfield Farm          375g
1                               170g
2                                   
3               Lacura         750ml
4                                   
5                                   
6        Nature's Glen              
7                                   
8                                   
9                                   
10       Ashfield Farm          450g
11  Specially Selected              
12  Specially Selected              
13                                  
14                                  
15                                  
16                                  
17 

我很难开始使用这个,有什么帮助吗?

1 个答案:

答案 0 :(得分:3)

将brand_size转换为记录字典并将其加载到数据框中。检查以下代码。并在适当的地方进行更改

DELIMITER //
CREATE PROCEDURE SellItem(pCharacterName varchar(50), pItemName varchar(50), howManyItems int(11))
BEGIN

DECLARE pchar int(11);
DECLARE pitem int(11);
DECLARE pitemCost int(11);
DECLARE pcharMoney int(11);
DECLARE newMoneyValue int(11);
DECLARE cnt int(11) DEFAULT 1;
DECLARE pcharitid int(11);

SET pchar=(SELECT `getChar`(`pCharacterName`));
SET pitem=(SELECT `getItem`(`pItemName`));
SET pitemCost=(SELECT `price` FROM `item` WHERE `item`.`item_id`=pitem);
SET pcharMoney=(SELECT `money` FROM `character` WHERE `character`.`character_id`=pchar);
SET newMoneyValue=pcharMoney;
WHILE cnt<=`howManyItems` DO
SET newMoneyValue=newMoneyValue+pitemCost;
SET cnt=cnt+1;
END WHILE;
SET cnt=1;

WHILE cnt<=`howManyItems` DO
SET pcharitid=(SELECT MAX(`character_item_id`) FROM `character_item` WHERE `character_item`.`character_id`=pchar AND `character_item`.`item_id`=pitem);
IF (SELECT `item_id` FROM `character_item` WHERE `character_item`.`item_id` IN(SELECT `item_id` FROM `item` WHERE `item`.`name`=`pItemName`)) THEN
DELETE FROM `character_item` WHERE `character_item`.`character_item_id`=pcharitid;
UPDATE `character` SET `money`=newMoneyValue WHERE `character`.`character_id`=pchar;
SET cnt=cnt+1;
END IF;
END WHILE;
END; //
DELIMITER ;