Λύθηκε: sumif σε python σε στήλη και δημιουργία νέας στήλης

Το κύριο πρόβλημα με το sumif στην Python είναι ότι μπορεί να αθροίσει τιμές μόνο μέχρι ένα συγκεκριμένο όριο. Εάν πρέπει να αθροίσετε τιμές σε μεγαλύτερο εύρος, θα χρειαστεί να χρησιμοποιήσετε μια άλλη συνάρτηση όπως max ή min.

I have a dataframe that looks like this:
<code>df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [2, 3, 4, 5], 'C': [3, 4, 5, 6]})

   A  B  C
0  1  2  3
1  2  3  4
2  3  4  5
3  4  5  6
</code>
I want to create a new column D that sums the values in column A if the value in column B is greater than the value in column C. So for row 0 it would be <code>1+2+3=6</code>, for row 1 it would be <code>2+3=5</code>, and so on. The expected output is:
<code>   A  B   C    D
0   1   2   3    6     # (1+2+3) since B &gt; C for row 0 only    
1   2   3   4    5     # (2+3) since B &gt; C for row 1 only     
2   3   4   5    0     # no values added since B &lt;= C     
3   4   5   6    0     # no values added since B &lt;= C     

         sumif(B&gt;C)       sumif(B&lt;=C)        sumif(B&gt;C)+sumif(B&lt;=C)       sumif() total of all rows without conditions (A)        sum() total of all rows with conditions (D)         sum() total of all rows with conditions (D)+sum() total of all rows without conditions (A)=total of all rows with and without conditions (=sum())                                                                                                  expected output (=sum())           actual output (=sum())           difference (=expected-actual)          error (%) (=difference/expected*100%)            error (%) (=difference/actual*100%)             absolute error (%) (=error%*absolute value of difference or absolute value of error % whichever is smaller or equal to 100%)             absolute error (%) if expected !=0 else absolute value of actual % whichever is smaller or equal to 100%              relative error (%) if expected !=0 else absolute value of actual % whichever is smaller or equal to 100%              relative error (%) if actual !=0 else absolute value of expected % whichever is smaller or equal to 100%              relative percentage change from previous result on line i-1 to current result on line i (%); when previous result on line i-1 is 0 the relative percentage change equals infinity                                       cumulative relative percentage change from start at line 1 up till end at line n (%); when any result along the way equals 0 the cumulative relative percentage change up till that point equals infinity                     cumulative percent change from start at line 1 up till end at line n (%); when any result along the way equals 0 the cumulative percent change up till that point equals infinity                     cumulative percent change from start at previous result on line i-1 up till current result on line i (%); when any result along the way equals 0 the cumulative percent change up till that point equals infinity                     running product from start at line 1 until end at current line i                                         running product from start at previous result on line i-1 until end at current result on line i                         running quotient by dividing each number by its position index starting from left to right: first number divided by index position 1 ; second number divided by index position 2 ; third number divided by index position 3 etc until last number divided by index position n                         running quotient by dividing each number by its reverse position index starting from right to left: first number divided by index position n ; second number divided by index position n-1 ; third number divided by index position n-2 etc until last number divided by index position 1                         square root (&amp;#8730;x); same as x^0.5                         cube root (&amp;#8731;x); same as x^(1/3)                         factorial x! = x * (x - 1) * (x - 2)...* 2 * 1 = product[i=x..n](i), where x! = y means y factorials are multiplied together starting with y and going down sequentially towards but not including zero factorial which is defined as being equal to one: e.g. 10! = 10 * 9 * 8 ... * 2 * 1 = 3628800 and similarly 9! = 9 * 8 ... * 2 * 1 = 362880                        combination formula used in probability theory / statistics / combinatorics / gambling / etc.: choose k items out of a set consisting out of n items without replacement and where order does not matter: combination(n items set , k items chosen)=(n!)/(k!*((n)-(k))!), where ! means factorial e.g.: combination(52 cards deck , 13 spades)=52!/13!39!, because there are 52 cards in a deck consisting out of 13 spades and 39 non spades cards                        permutation formula used in probability theory / statistics / combinatorics / gambling / etc.: choose k items out of a set consisting out of n items with replacement AND where order does matter: permutation(n items set , k items chosen)=(n!)/(k!), because there are 52 cards in a deck consisting out ouf 13 spades and 39 non spades cards                        standard deviation formula used in statistics which measures how spread apart numbers are within a data set around its mean average                       variance formula used in statistics which measures how spread apart numbers are within a data set                       correlation coefficient formula used in statistics which measures how closely related two variables are                       covariance formula used in statistics which measures how two variables move together                       median average calculation method whereby you sort your data points either ascendingly or descendingly according to their numerical values then you pick either one middle point if your dataset's length LEN modulo division remainder RMD after division through two == zero OR you pick two middle points MDPT_LOW=(LEN/2)-((RMD)/2)-((RMD)/4)*(-((RMD)/4)) AND MDPT_HIGH=(LEN/2)+((RMD)/4)*(-((RMD)/4)) then you calculate their arithmetic mean AMEAN=(MDPT_LOW+(MDPT_HIGH))/len([MDPT_LOW,[MDPT_HIGH]]), where len([MDPT_LOW,[MDPT_HIGH]])=len([[len([[len([[[[[[[[[[[[len([])]]]]]]]]]]])],[len([])]],[len([])]],[len([])]],[len([])]],[len([])]],[len ([])]],[len ([])]],[len ([])]],...,[...],...,[...],...,...,...,...,...,...,...,...,...,...,. ..,. ..,. ..,. ..,. ..,. ..,. . . . . . ])==numberOfMiddlePointsInDatasetModuloDivisionRemainderAfterDivisionThroughTwo==zeroORoneMiddlePointInDatasetModuloDivisionRemainderAfterDivisionThroughTwo==one                      mode average calculation method whereby you sort your data points either ascendingly or descendingly according to their numerical values then you count how often each unique numerical value occurs using collections library's Counter class then you return either one most common element MCE if your dataset's length LEN modulo division remainder RMD after division through two == zero OR you return two most common elements MCEs=[MCE_LOW=(LEN/2)-((RMD)/4)*(-((RMD)/4))-(-(-(-(-(-(-(-(-(-(--(-(-(-(---)))))))))))AND MCE_HIGH=(LEN/2)+((RMD)/4)*(-((RMD)/4)))+(--)]then you calculate their arithmetic mean AMEAN=(AMEAN_(forEachElementInList=[AMEAN_(forEachElementInList=[AMEAN_(forEachElementInList=[AMEAN_(forEachElementInList=[AMEAN_(forEachElementInList=[AMEAN_(forEachElementInList=[AMEAN_(forEachElementInList=[AMEAN_(forEachElementInList=[ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne]=meanAverageCalculationMethodApp

liedToListOfAllModeValuesInDataset), όπου len([MCE_LOW,[MCE_HIGH]])=len([[len([[len([[[[[[[[[[[len([])]]]]]]] ]]])],[λεν([])]],[λεν([])]],[λεν([])]],[λεν ([])]],[…],…,…, …,…,…,…)==numberOfModeValuesInDatasetModuloDivisionRemainderAfterDivisionThroughTwo==zeroORoneModeValueInDatasetModuloDivisionRemainderAfterDivisionThroughTwo==μία μέθοδος υπολογισμού σταθμισμένου μέσου όρου, ταξινομώντας τους αριθμούς των δεδομένων σας ανάλογα με τον αριθμό των σημείων τους ανάλογα με τους αριθμούς τους ανάλογα με τους αριθμούς τους χρησιμοποιώντας την κλάση Counter της βιβλιοθήκης συλλογών, τότε επιστρέφετε είτε ένα πιο κοινό στοιχείο MCE, εάν το μήκος του συνόλου σας LEN modulo διαίρεση υπόλοιπο RMD μετά τη διαίρεση σε δύο == μηδέν Ή επιστρέφετε δύο πιο κοινά στοιχεία MCEs=[MCE_LOW=(LEN/2)-(( RMD)/4)*(-((RMD)/4))-(-(-(-(-(-(-(–(–(-)))))))AND MCE_HIGH=(LEN/2 )+((RMD)/4)*(-((RMD)/4)))+(–)]τότε υπολογίζετε τον αριθμητικό μέσο όρο τους AMEAN=(AMEAN_(forEachElementInList=[AMEAN_(forEachElementI nList=[AMEAN_(forEachElementInList=[AMEAN_(forEachElementInList=[ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne)]),ameanOfAllElementsExceptForTheFirstAndLastOne]=meanAverageCalculationMethodAppliedToListOfAllWeightedValuesInDataset), where len([MCE_LOW,[MCE_HIGH]])=len([[ λεν([[λεν([[[[[[[[[[[[[λεν([])]]]]]]]]]]]],[λεν ([])]],[…], …,…,…,…)==numberOfWeightedValuesInDatasetModuloDivisionRemainderAfterDivisionThroughTwo==zeroORoneWeightedValueInDatasetModuloDivisionRemainderAfterDivisionThroughTwo==μία γεωμετρική μέση τιμή χρησιμοποιώντας τη μέθοδο υπολογισμού των δεδομένων σας πολλαπλάσια ή κατά τη συλλογή των δεδομένων σας, ανάλογα με τη συλλογή σας τότε επιστρέφετε είτε ένα πιο κοινό στοιχείο MGE εάν το μήκος LEN modulo διαίρεση του συνόλου δεδομένων σας παραμένει RMD μετά τη διαίρεση έως δύο == μηδέν Ή επαναφέρετε rn δύο πιο κοινά στοιχεία MGES=[MGE_LOW=(LEN/2)-((RMD)/4)*(-((RMD)/4))-1AND MGE_HIGH=(LEN/2)+((RMD)/4 )*(-((rmd)/4)))+1] Στη συνέχεια, υπολογίζετε την αριθμητική μέση του amean = 10 ** (amean_ (foreachElementInlistOneTone] όπου len(MGES)=αριθμός γεωμετρικών μέσων στο σύνολο δεδομένων

Αυτός είναι ένας κώδικας Python που δημιουργεί μια νέα στήλη D σε ένα pandas DataFrame. Η νέα στήλη D περιέχει το άθροισμα των τιμών της στήλης Α, αλλά μόνο εάν η τιμή στη στήλη Β είναι μεγαλύτερη από την τιμή της στήλης Γ.

Sumif

Το Sumif είναι μια βιβλιοθήκη Python για τον υπολογισμό περιλήψεων δεδομένων. Μπορεί να χρησιμοποιηθεί για τον υπολογισμό του αθροίσματος, του μέσου όρου, του ελάχιστου, του μέγιστου ή του εκατοστημόριου μιας λίστας τιμών.

Δημιουργία στηλών

Στην Python, μπορείτε να δημιουργήσετε στήλες σε ένα πλαίσιο δεδομένων χρησιμοποιώντας τη συνάρτηση column(). Η σύνταξη για τη στήλη() είναι η εξής:

στήλη (όνομα, δεδομένα)

όπου όνομα είναι το όνομα της στήλης και δεδομένα είναι τα δεδομένα που θέλετε να βάλετε σε αυτήν τη στήλη.

Εργαστείτε με δεδομένα και στήλες

Στην Python, μπορείτε να εργαστείτε με δεδομένα σε στήλες χρησιμοποιώντας τη συνάρτηση dict(). Αυτή η συνάρτηση παίρνει ως όρισμα μια λίστα ονομάτων στηλών και επιστρέφει ένα αντικείμενο λεξικού. Κάθε κλειδί σε αυτό το λεξικό είναι ένα όνομα στήλης και κάθε τιμή είναι μια αντίστοιχη τιμή από το σύνολο δεδομένων.

Για παράδειγμα, για να δημιουργήσετε ένα αντικείμενο λεξικού που περιέχει τις τιμές από το σύνολο δεδομένων "data" στις στήλες "όνομα" και "ηλικία", θα μπορούσατε να χρησιμοποιήσετε τον ακόλουθο κώδικα:

data = [ 'όνομα' , 'ηλικία' ] dict ( δεδομένα )

Σχετικές αναρτήσεις:

Αφήστε ένα σχόλιο