根据许多列和值从数据框中获取行

时间:2020-01-22 15:58:02

标签: python pandas

这是一个非常简单的娱乐活动,尽管真正的DF有更多的列

我的数据框:

    length  width  height  age
0        1      5       8   12
1        1      5       8   12
2        1      5       8   21
3        1      5       8   15
4        1      5       8   15
5        1      6       9   12
6        2      6       9   32
7        2      6       9   32
8        2      6       7   98
9        3      4       7   12
10       3      4       7   54
11       3      4       7   21

我想获取width == 6 age ==32所在的行。

足够简单:

d[(d['width']==6) & (d['age']==32)]

   length  width  height  age
6       2      6       9   32
7       2      6       9   32

是否有一种方法可以使这一过程更加自动化?假设我有一个列和值的列表。在这种情况下,它仍然仍然只有两个列/值,但是我正在考虑处理15个或更多:

cols = ['width','age']
vals = [6,32]

现在要构建一个空的数据框并使用append更新行:

df_temp = pd.DataFrame()

for col,val in zip(cols,vals):


    if df_temp.empty:

        df_temp = df[df[col]==val]

    else:

        df_temp.append(df[df[col]==val])


   length  width  height  age
5       1      6       9   12
6       2      6       9   32
7       2      6       9   32
8       2      6       7   98

这相当于使用or符号|

d[(d['width']==6) | (d['age']==32)]

如何自动执行此操作,使其成为AND而不是or

我已经尝试了一些完全令人发指的方法,但是它不起作用,它似乎仍然等效于|而不是&

[d[(d[col]==val) & (d[col]==val)] for col, val in zip(cols,vals)][0]

   length  width  height  age
5       1      6       9   12
6       2      6       9   32
7       2      6       9   32
8       2      6       7   98

我的可复制数据框:

import pandas as pd

pd.DataFrame({'length': pd.Series([1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3],dtype='int64',index=pd.RangeIndex(start=0, stop=12, step=1)), 'width': pd.Series([5, 5, 5, 5, 5, 6, 6, 6, 6, 4, 4, 4],dtype='int64',index=pd.RangeIndex(start=0, stop=12, step=1)), 'height': pd.Series([8, 8, 8, 8, 8, 9, 9, 9, 7, 7, 7, 7],dtype='int64',index=pd.RangeIndex(start=0, stop=12, step=1)), 'age': pd.Series([12, 12, 21, 15, 15, 12, 32, 32, 98, 12, 54, 21],dtype='int64',index=pd.RangeIndex(start=0, stop=12, step=1))}, index=pd.RangeIndex(start=0, stop=12, step=1))

2 个答案:

答案 0 :(得分:3)

这是将assigndf.eq一起使用locall的方法;

df[df.eq(df.assign(**dict(zip(cols,vals)))).loc[:,cols].all(1)]

   length  width  height  age
6       2      6       9   32
7       2      6       9   32

答案 1 :(得分:2)

我们可以通过在此处使用基础的numpy数组来简化此操作:

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using TestStack.White;
    using TestStack.White.Factory;
    using TestStack.White.UIItems;

    namespace UnitTestProject3
    {
        [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                using (var application = Application.Launch("Calc.exe"))
                {
                    var calculator = application.GetWindow("Calculator", InitializeOption.NoCache);

                    // do something with the application
                    var b7 = calculator.Get<Button>(TestStack.White.UIItems.Finders.SearchCriteria.ByText("7"));
                    b7.Click();

                    application.Close();

                }
            }
        }
    }