English 中文(简体)
粉碎 Pandas Multi-Index。 某一多指数主要标题的多指数小标题的数目
原标题:Python Pandas Multi-Index. Count the number of multi-index sub-headings for a given multi-index main heading

我需要计算某一多指数主要标题的多指数小标题的数目。

This question is related to my previous question here: read_csv: Multiple Header Rows. Single header row has duplicates within it, but no column is duplicated when both header rows considered If that link doesn t pass, the search term on stackoverflow would be: "read_csv: Multiple Header Rows. Single header row has duplicates within it, but no column is duplicated when both header rows considered"

采用同样的样本目录:

Sample csv file:

Exp,2023-09-22,2023-09-22,2023-09-29,2023-10-06,2023-10-13  
Code,A,B,A,A,A  
Item,,,,,  
260,1,8,17,31,42  
270,2,9,18,32,43  
280,3,10,19,33,44  
290,4,11,20,34,45  
300,5,12,21,35,46  

And reading the csv into a data frame like this:

Gives Output:

print(df)

Exp  2023-09-22     2023-09-29 2023-10-06 2023-10-13
Code          A   B          A          A          A
Item                                                
270           2   9         18         32         43
280           3  10         19         33         44
290           4  11         20         34         45
300           5  12         21         35         46


Question: How do I count the number of "Code" entries for a given Exp heading? For example: How many Code entries are listed for Exp 2023-09-22 ? Answer: 2 (they are A and B ) How many Code entries are listed for Exp 2023-10-13 ? Answer: 1

令人非常赞赏的是,任何帮助掩盖了这一问题。

我曾尝试过各种镜头(df.columns[col_idx])和其他器具,但我没有看到任何东西。 关于可接受的解决办法,我要么用数字,如图2或图1,要么用我随后可以计算的姓名或价值清单(或无论它们是否认为)。 无论如何,这些法典要么是“A”要么是“B”,要么是“A”&”B”。

问题回答

Code

单位+规模(按栏目0级)

df.groupby(level=0, axis=1).size()

产出:

Exp
2023-09-22      2
2023-09-29      1
2023-10-06      1
2023-10-13      1

如果贵国的法典清单不是独一无二的(例如,如果是A、A、B在2023-09-22下,如果你想回去2),则按法典使用。

df.droplevel(level=-1, axis=1).columns.to_frame(index=False).groupby( Exp )[ Code ].nunique()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...