基于python中的现有数据框列创建多个新数据帧

时间:2018-05-22 21:01:39

标签: python pandas dataframe

我有一个pandas数据框df,它有4列和很多行。

我想根据数据框的一列的值创建5个不同的数据框。我引用的列名为color

color有5个唯一值:redbluegreenyelloworange

我想要做的是,5个新数据框中的每一个都应该包含color中包含值的所有行。例如,df_blue应该包含所有行和列,其他数据框中color列的值为蓝色。

我的代码如下:

# create 5 new data frames
df_red = []
df_blue= []
df_green= []
df_yellow= []
df_orange= []
for i in range(len(df)):
    if df['color'] == "blue"
       df_blue.append(df)

# i would do if-else statements to satisfy all 5 colors

我觉得我错过了一些逻辑......有什么建议或意见吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

您需要使用$('#dialog_confirm').dialog({ autoOpen: false, modal: true, draggable: false, resizable: false, show: { effect: "blind", duration: 500 }, hide: { effect: "fade", duration: 500 }, buttons: { 'Confirm' : { text:'Confirm', id:'confirm_button', click:function() { $(this).dialog('close'); } }, 'Cancel' : { text:'Cancel', id:'cancel_button', click:function() { $(this).dialog('close'); } } } }); $('#remove_domain_form').submit(function (e) { e.preventDefault(); //var isEmpty = false; $(':input:not(button)').each(function () { if ($(this).val() === "") { var error_text = $('#dialog p').text("All fields are required"); $('#dialog').html(error_text); $('#dialog').dialog('option', 'title', 'Error').dialog('open'); return false; } else { $('#dialog_confirm').dialog('open'); } }); $('#confirm_button').click(function() { console.log("Not Empty"); $.ajax({ url: "check-domain-remove.php", type: "post", dataType: 'json', data: $('#remove_domain_form').serialize(), success: function (response) { if (response === "Success") { //DO OTHER STUFF var success_div = '<p class=\"input--success\">Domain Successfully Removed</p><br><br>'; $('body').append(success_div); } } }); }); }); 。以下代码片段创建一个示例DataFrame并将其转换为字典,其中颜色是键,匹配的数据帧是值:

groupby

答案 1 :(得分:-1)

我最终为每种颜色做了这个。

  blue_data = data[data.color =='blue']
相关问题