根据条件使用字典填充Pandas DataFrame

时间:2019-01-02 23:52:23

标签: python pandas dataframe assign

我有一个DataFrame

>> test = pd.DataFrame({'A': ['a', 'b', 'b', 'b'], 'B': [1, 2, 3, 4], 'C': [np.nan, np.nan, np.nan, np.nan], 'D': [np.nan, np.nan, np.nan, np.nan]})
    A   B   C   D
0   a   1       
1   b   2       
2   b   3       
3   b   4       

我也有一个字典,其中b中的input_b表示我只在row.A = b处修改行。

>> input_b = {2: ['Moon', 'Elephant'], 4: ['Sun', 'Mouse']}

如何使用字典中的值填充DataFrame以获得

    A   B   C       D
0   a   1       
1   b   2   Moon    Elephant
2   b   3       
3   b   4   Sun     Mouse

4 个答案:

答案 0 :(得分:1)

这可能不是最有效的解决方案,但据我了解,它可以完成工作:

int ControlsAdded = 0; 

protected void button_Click(object sender, EventArgs e)
{
    TextBox selectedText = new TextBox();
    selectedText.Size = new Size(300, this.Font.Height);
    selectedText.Location = new Point(100, ControlsAdded * selectedText.Height + 30);
    ControlsAdded += 1;
    this.Controls.Add(selectedText);
    selectedText.BringToFront();

    using (var fBD = new FolderBrowserDialog())  {
        if (fBD.ShowDialog() == DialogResult.OK)
            selectedText.Text = fBD.SelectedPath;
    }
}

收益:

import pandas as pd
import numpy as np

test = pd.DataFrame({'A': ['a', 'b', 'b', 'b'], 'B': [1, 2, 3, 4],
                     'C': [np.nan, np.nan, np.nan, np.nan], 
                     'D': [np.nan, np.nan, np.nan, np.nan]})


input_b = {2: ['Moon', 'Elephant'], 4: ['Sun', 'Mouse']}


for key, value in input_b.items():
    test.loc[test['B'] == key, ['C', 'D']] = value

print(test)

如果字典 A B C D 0 a 1 NaN NaN 1 b 2 Moon Elephant 2 b 3 NaN NaN 3 b 4 Sun Mouse 太大(正在更新的行太多,for循环中的迭代次数过多),则速度会变慢,但是对于input_b较小的偶数,它应该相对较快具有大型input_b数据帧。

此答案还假设test字典中的键引用原始数据帧中input_b列的值,并将在B和{{1 }}列中的C列中的重复值。

答案 1 :(得分:1)

使用update

test=test.set_index('B')
test.update(pd.DataFrame(input_b,index=['C','D']).T)
test=test.reset_index()
test
   B  A     C         D
0  1  a   NaN       NaN
1  2  b  Moon  Elephant
2  3  b   NaN       NaN
3  4  b   Sun     Mouse

答案 2 :(得分:1)

将索引设置为B后,可以使用loc索引:

test = test.set_index('B')
test.loc[input_b, ['C', 'D']] = list(input_b.values())
test = test.reset_index()

print(test)

   B  A     C         D
0  1  a   NaN       NaN
1  2  b  Moon  Elephant
2  3  b   NaN       NaN
3  4  b   Sun     Mouse

答案 3 :(得分:1)

使用<script> $(document).ready(function() { var itemlist = document.getElementsByClassName("costbasedonotherservice"); var basepricearray = []; for (var i=0; i < itemlist.length; i++) { var baseprice = itemlist[i].innerText; basepricearray.push(baseprice); } var multiplier = $('#costsbasedonthisservice select :selected').attr('value'); $('#costsbasedonthisservice select').on('change', function() { var multiplier = $('#costsbasedonthisservice select :selected').attr('value'); for (var i=0; i < itemlist.length; i++) { var newprice = basepricearray[i]*multiplier; var newprice2 = Number(newprice).toFixed(2); var wrapper = itemlist[i].parentElement; if (newprice2 == 0){ wrapper.classList.add("hidepricing100"); } else ( **wrapper.classList.remove("hidepricing100");** itemlist[i].innerText = newprice2; ) } }); }); </script>

apply

收益

test['C'] = test['B'].map(input_b).apply(lambda x: x[0] if type(x)==list else x)
test['D'] = test['B'].map(input_b).apply(lambda x: x[1] if type(x)==list else x)