score:1
Avoiding a one-liner will improve readability, making it a little less confusing:
mask = (csv_pd.setA==1) & (csv_pd.setB==0) & (csv_pd.setC==0)
csv_pd[mask].groupby('D').count()
Another possibility, which happens to be a one-liner, is to use the query
method:
csv_pd.query('setA==1 & setB==0 & setC==0').groupby('D').count()
Note also that you can pass a column name to groupby
, instead of Series values. Thus, groupby('D')
instead of groupby(csv_pd.D)
.
To count the size of all possible subsets, the powerset
recipe and itertools.product
would be helpful:
import itertools as IT
import numpy as np
import pandas as pd
def powerset(iterable, reverse=False, rvals=None):
"""powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"""
s = list(iterable)
N = len(s)
if rvals is None:
rvals = range(N, -1, -1) if reverse else range(N + 1)
return IT.chain.from_iterable(
IT.combinations(s, r) for r in rvals)
df = pd.DataFrame(np.random.randint(2, size=(10,4)), columns=list('ABCD'))
print(df)
for cols in powerset(df.columns):
if not cols: continue
for vals in IT.product([0,1], repeat=len(cols)):
mask = np.logical_and.reduce([df[c]==v for c, v in zip(cols, vals)])
cond = ' & '.join(['{}={}'.format(c,v) for c, v in zip(cols,vals)])
n = len(df[mask])
print('n({}) = {}'.format(cond, n))
yields
n(A=0) = 8
n(A=1) = 2
n(B=0) = 4
n(B=1) = 6
...
n(A=0 & B=0) = 4
n(A=0 & B=1) = 4
n(A=1 & B=0) = 0
...
n(A=1 & B=1 & C=0 & D=0) = 0
n(A=1 & B=1 & C=0 & D=1) = 1
n(A=1 & B=1 & C=1 & D=0) = 0
n(A=1 & B=1 & C=1 & D=1) = 1
Source: stackoverflow.com
Related Query
- How to use python pandas to get intersection of sets from a csv file
- How to pull information from a CSV file for use in a stacked bar chart
- How to load data from a CSV file in D3 v5
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()
- How do I use d3.domain to get d3.min and d3.max from multiple columns in a JSON file?
- how to get data with tsv or csv to array in d3.js from a txt file?
- How to add links from CSV file to SVG elements generated with D3?
- How to load a csv file from static in django into javascript function
- How to use nest in d3 to group using the 'month' as key, but my csv file contains the whole date?
- How do I convert strings from csv in d3.js and be able to use them as a dataset?
- How to create an array of nested objects from a csv file in D3.js?
- How to get a data from an analysis in R in Json file format to get D3 visualisations?
- how to convert data selected from a postgres database to json or csv to use it with d3js lib?
- In Meteor, how can I import data from a .tsv file to use in a d3.js chart?
- How to make a scatter plot from a CSV file using d3.js?
- D3.js - Load data from CSV file and use it outside function call
- how to transpose data from csv file in d3.js
- How to load csv file to use with D3
- How to select only x rows from a csv file for a pie chart
- How to update data on a page according to data from a CSV file instead of using fixed element data on the page?
- How can I parse csv data from a javascript array of strings for use in d3 graphs
- Get the number of connected nodes from a csv file that shows their relations
- How to use a local directory csv file instead of hard coding in the codes?
- How do I retrieve an array from a csv file in javascript using d3?
- How to plot data to map from csv file in D3.js
- How to sort number columns from csv file read in using D3.js - columns read in as strings
- How to extract data from a csv file and create a line chart from it in D3.js
- How to get most recent csv file in Ajax call?
- How to use D3.js to visualize JSON returned from my app's GET request
- How to read data generated from REST API in a Javascript file and use it another?
More Query from same tag
- Hyperlinks in D3.js objects, part 2
- Unable to align the svg:g of d3 element inside the div
- x coordinate value after drag and drop event
- D3 graph circle packing graph coloring
- D3.js edge bundle chart will not display inside a CSS grid position
- Create directional arrows on an SVG path using D3
- on click of path in d3 enlarge it
- d3js: why doesn't first bar disappear using exit method?
- Selecting duplicate id's from an SVG exported from Illustrator in d3.js
- How to pass data to d3.json()
- D3: combine circles and images
- Databinding a D3 slider with Knockout
- D3 Library — How To Access Data from JSON in this example
- d3 output includes values outside domain
- Returning array from d3.json()
- Dashed line transforms the null values into zeros on the chart
- Bind with d3 events
- Force simulation of bubbles and texts going unbounded and inconsistent with XY axis
- D3.js Ordinal Scale Axis mis alignment
- How to create progress bars?
- D3 .append() with variable
- Pass argument to function
- width attribute not working with d3 style
- How to zoom only on the X axis in a line chart in d3.js (v7)
- How to access nested data Values in D3
- remove a legend dimple.js
- Manipulating D3 axis labels
- How to tell when a D3 force layout has stopped
- How to create elements (links) with 2 or more colors with CSS to make it look like a rainbow?
- D3 stacked bar chart not able to see full x axis lables