简体   繁体   English

如何添加键数未知的字典系列

[英]How to add series of dictionary with unknown number of keys

I have created following dictionary test , consisting of Series objects: 我创建了以下由Series对象组成的字典test

test = {
    'A': pd.Series([True, False, True]),
    'B' : pd.Series([True,False,False])
}

I would like to perfrom test['A'] & test['B'] . 我想执行test['A'] & test['B'] My problem is that I want to perfrom bit-wise addition for any possible number of keys in the dictionary. 我的问题是我想对字典中任意数量的键进行按位加法。 (Ie it can be 'A' or 'A' and 'B' or 'A' and 'B' and 'C' etc.). (即可以是'A''A' and 'B''A' and 'B' and 'C'等)。 In any case, the value for each key has the same length, and all the Series are boolean. 无论如何,每个键的值都具有相同的长度,并且所有Series都是布尔值。

There are many advantages to working with a DataFrame instead of a dictionary of Series objects. 使用DataFrame代替Series对象的字典有很多优点。 Converting from the latter to the former is trivial: 从后者转换为前者是微不足道的:

>>> df = pd.DataFrame(test)
>>> df
       A      B
0   True   True
1  False  False
2   True  False

While the DataFrame constructor is pretty smart about parsing the input data, you could explicitly tell it you are initializing from a dictionary by using the from_dict classmethod : 尽管DataFrame构造函数在解析输入数据时非常聪明,但是您可以使用from_dict classmethod明确地告诉它您正在从字典进行初始化:

>>> df = pd.DataFrame.from_dict(test)

Now you can apply & using the all method along any axis you want: 现在,您可以沿任意轴应用&使用all方法:

>>> df.all(axis=1) # going across
0     True
1    False
2    False
dtype: bool

The same goes for | | using any : 使用any

>>> df.any(axis=1)
0     True
1    False
2     True
dtype: bool

There is a simple one-line solution to your problem (if you want cumulative and operation over columns eg A and B , A and B and C , A and B and C and D and so on): 有一个简单的单线解决方案可以解决您的问题(如果您想对列A and BA and B and CA and B and C and D等等进行累加and运算):

import pandas as pd

test = {
    "A": pd.Series([True, True, True]),
    "B": pd.Series([True, False, False]),
    "C": pd.Series([False, True, False]),
    "D": pd.Series([True, False, False]),
}

df = pd.DataFrame.from_dict(test)

# Here is da man    
print(df.cummin(axis="columns"))

Using cummin , if any value is False , all coming after it will be False as well as it's the smallest value. cummin ,如果任何值是False ,所有到来之后会False ,以及它的最小值。

Original dataframe: 原始数据框:

      A      B      C      D
0  True   True  False   True
1  True  False   True  False
2  True  False  False  False

Cumulative and : 累计and

      A      B      C      D
0  True   True  False  False
1  True  False  False  False
2  True  False  False  False

First column is A , second is A and B , third A and B and C , last is A and B and C and D . 第一列是A ,第二列是A A and B ,第三列是A A and B and C ,最后一列是A and B and C and D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM