Home > other >  [for] how to implement the Dataframe several digital sum of a column in another column in the same a
[for] how to implement the Dataframe several digital sum of a column in another column in the same a

Time:10-06

There are the following Dataframe:
Dr Cr
A 10 0
B 0 1
0 6 c
D 2 0
E 3 0
7 0 f
5 g 0
H 2 0
I 0 3
J 0 5

To achieve the effect of is (for a non-zero number) :

1. Find the Dr And Cr column in the same Numbers, output line where the Numbers:
D 2 0
H 2 0

E 3 0
I 0 3

2. Find Cr column add up a few Numbers is equal to the Dr A certain number of all combinations of the columns:
A 10 0
5 g 0
J 0 5

A 10 0
B 0 1
0 6 c
I 0 3

7 0 f
5 g 0
H 2 0

7 0 f
H 2 0
J 0 5
No exhaustive, is similar to the effect...

About the function of the first, my own ideas are as follows:
 for I in the list (df [' Cr ']) : 
If I==0:
The continue

If I in the list (df [' Dr ']) :
Df1=df. Iloc [np. Flatnonzero (df [' Dr ']==I)]
Df2=df. Iloc [np. Flatnonzero (df==I) [' Cr ']]
Df=pd concat ([df1, df2])
Print (df)


But the results are as follows, don't know why only show the first match is:
Dr Cr
D 2 0
H 2 0

And about the second function... At present there is no clue... Is the need to use recursion?

360 - degree flip kneel ground kowtow for help!!!!!!

CodePudding user response:

Second,
I think so:
Traverse the df
Screening of all Cr is smaller than the current Dr Data. Using a recursive change change matching algorithm to calculate data.
  • Related