str.ljust() not producing changes to pandas column(series) (using to sort values) - string

Cabin_Fare.Cabin.head(20) (produces these results)
583 A10
208 A11
475 A14
556 A16
331 A18
284 A19
599 A20
28 A21
630 A23
867 A24
647 A26
112 A29
209 A31
185 A32
445 A34
293 A34
374 A34
806 A36
96 A5
23 A6
I assign it to x and convert the object types to string type.
x = Cabin_Fare.Cabin.astype('string')
I'm trying to push values like A5/A6 (The last two values) one space to the left, because when the column is being sorted, any values with only a len of 2 aren't being sorted properly. I'm assuming because they aren't aligned equally with those values having a len of 3.
So I tried to run this code but I'm not seeing any changes made (the A5/A6 aren't being pushed one space to the left)
for i in x[x.notnull()]:
if len(i) == 2:
i= i.ljust(3,)
Edit: I'm trying to utilize Boud's solution and I'm running into a problem because there are values/instances where only the letter (no numbers)is present.
The error shows up as:
ValueError: invalid literal for long() with base 10: ''
To circumvent this, I'm trying to add a '0' to the values where only the letter is present.
for i in x:
if len(i)==1:
i = i+'0'
However, the changes are not sticking outside of the loop, just within.

Your values don't have a leading space, actually. Sorting against strings will apply the alphabetical order, which is character by character. All strings start with an A, then the second character is a digit, and 5 and 6 are digits that are greater than 0, 1, 2, and 3 in your example. So the numbers are considered are numbers, but as a sequence of single digit.
If you want a sort by the numbers following A, extract the number by removing the first character, convert into int, sort that series of int, and then reindex x based on the resulting index of that proper sort:
x.reindex(x.str[1:].astype(int).sort_values().index)
Out[57]:
18 A5
19 A6
0 A10
1 A11
2 A14
3 A16
4 A18
5 A19
6 A20
7 A21
8 A23
9 A24
10 A26
11 A29
12 A31
13 A32
14 A34
15 A34
16 A34
17 A36
Name: Cabin, dtype: object

Related

Replace and update columns in excel

Table
A B C D E
1 Jan Feb Mar Apr Jun
2 24 32 64 54 45
3 66 76 76 76 87
4 45 65 84 67 23
the last 3 columns should automatically update in another column as
Last 3 moths data
AN AO AP
1 Month1 Month2 Month3
2 64 54 45
3 76 76 87
4 84 67 23
If I add another column in the first table as july in columns F. Then it should automatically replace the data in Month3 in second table and month3 data sholud move to month2 and month2 to month1.
=IF(AN2="",AL2,IF(AO2="",AM2,IF(AP2="",AN2,IF(AQ2="",AO2,IF(AQ2<>"",AP2,IF(AP2<>"",AO2,IF(AO2<>"",AN2,IF(AN2<>"",AM2,""))))))))
The formula is simlar to the another columns.
With any version of Excel:
You can detect where the columns end using many methods - the simplest is using Count.
Enter the following into the top left of your result table:
=INDEX(2:2,,COUNT(2:2)-2)
This counts the number of cells with data in, and returns the data from that numbered column (offset to the left by 2). In the next cell to its right, change the -2 to -1. In the last cell, change the -1 to -0 or remove that part entirely.
Then copy the cells down to match your source table.
If you have Excel-365 then use below formula.
=INDEX(A1:Z4,SEQUENCE(ROWS(A1:A4)),SEQUENCE(1,3,MAX(IF(A1:Z1<>"",COLUMN(A1:Z1),""))-2))
If you don't have Excel-365 then use below formula then drag down and across as required.
=INDEX($A$1:$Z$4,ROW($A1),MAX(IF($A$1:$Z$1<>"",COLUMN($A$1:$Z$1),""))-3+COLUMN(A$1))
If you have ms365, try to use FILTER(), for example:
Formula in G1:
=FILTER(Table1[#All],COLUMN(Table1[#Headers])>COLUMNS(Table1[#Headers])-3)
If you add columns:
If you remove columns:

Order the numbers of each row

I have a formula =SMALL(RIGHT($A3:$f3,1),{1,2,3,4,5,6}) and it works fine to extract one digit from the data.
Now I have a different set of data but I couldn't make it work with these data.
The data I have is like this:
L18 L2 R3 R19 R21 R22
L23 L12 L15 L17 L2 R13
L11 L17 L2 R2 R9 R24
L22 L5 L9 R9 R24 R27
With the function, I should be able to get these results:
2 3 18 19 21 22
2 12 13 15 17 23
2 2 9 11 17 24
5 9 9 22 24 27
So they have one letter and 1 or 2 digit numbers at the end. For each row, I would like to extract the digits and arrange them from smallest to largest. How can I do that?
Try this:
=SMALL(--MID($A1:$F1,2,255),{1,2,3,4,5,6})
Given your initial formula I presume you know that this is an array formula and needs to be entered using Ctrl+Shift+Enter
you can do this in two Phase
As given below
=VALUE(MID(A2,LEN(LEFT(A2,1))+1,LEN(A2)-1))
=SMALL($A8:$F8,1)

How to paste list of values into cells of same columns based on equal intervals in excel?

Like i have list of some numbers and i wanted to paste those values in cells such as A1 which get first value then A9 get second value similary.
I want solution of this without using macros.
A B C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
The value which I have are:-
87
75
67
74
94
99
98
80
64
65
59
77
97
99
so I wanted that value 87 goes to A1 then 75 goes to A9 then 67 goes to A18 and so on..
Start in B1: =A1 just for the first (your list is in A1:A14 for example)
In B2 write the following:
=IF(MOD(ROW(),9)=0,INDEX($A$1:$A$14,INT(ROW()/9)+1,1),"")
Mod(Row(),9)=0 to test if the row is the 9th after the precedent Value
Int(Row()/9) to get from the list in A the next row
IF to return empty
You can drag it down
When finish copy column B paste special Values in a new column and you can work with it normally

Excel SUMIF based on array using text string

Is there a way to substitute the cell address containing a text string as the array criteria in the following formula?
=SUM(SUMIF(A5:A10,{1,22,3},E5:E10))
So instead of {1,22,3}, "1, 22, 3" is entered in cell A2 the formula becomes
=SUM(SUMIF(A5:A10,A2,E5:E10))
I have tried but get 0 as a result (refer C16)
A B C D E F G H
1 Tree
2 {1,22,3} 1
3 22
4 Tree Profit 3
5 1 105
6 2 96
7 1 105
8 1 75
9 2 76.8
10 1 45
11
12 330 =SUM(SUMIF(A5:A10,{1,22,3},B5:B10))
13
14 330 =SUMPRODUCT(SUMIF(A5:A10,E2:E3,B5:B10))
15
16 0 =SUM(SUMIF(A5:A10,A2,B5:B10))
17 NB: Custom Format "{"#"}" on Cell A2 I enter 1,22,3 so it displays {1,22,3}
Ok so after some further searching (see Excel string to criteria) and trial and error I have come up with the following solution.
Using Name Manager I created UDF called GetList which Refers to:
=EVALUATE(Sheet1!$A$3) NB: Cell A3 has this formula in it =TEXT(A2,"{#}")
I then used the following formula:
=SUMPRODUCT(SUMIF($A$5:$A$12,GetList,$B$5:$B$12))
which gives the desired result of 321 as per the other two formulas (see D12 below).
If anyone can suggest a better solution then feel free to do so.
Thanks to Dennis to my original post regarding table
A B C D E
1 Tree
2 1,22,3 1
3 {1,22,3} =TEXT(A2,"{#}") 22
4 Tree Profit 3
5 11 105
6 22 96
7 1 105
8 3 75
9 2 76.8
10 1 45
11
12 321 =SUMPRODUCT(SUMIF($A$5:$A$12,GetList,$B$5:$B$12))
13
14 321 =SUM(SUMIF(A5:A10,{1,22,3},B5:B10))
15
16 321 =SUMPRODUCT(SUMIF(A5:A10,E2:E3,B5:B10))
17
18 0 =SUM(SUMIF(A5:A10,A2,B5:B10))
19 NB: Custom Format "{"#"}" on Cell A2 I enter 1,22,3 so it displays {1,22,3}

Excel table formulas to return blank

I have a table in excel with 2 columns, the E column is the running total the D column is the input value so normally it would be = E15+D16 however, i want the E column to return a blank if nothing is entered in the D column- what formula do I need?
1 Nov-23 Nov-30 1,230 1,230
2 Dec-01 Dec-07 130 1,360
3 Dec-08 Dec-14 416 1,776
4 Dec-15 Dec-21 124 1,900
5 Dec-22 Dec-28 102 2,002
6 Dec-29 Jan-04 83 2,085
7 Jan-05 Jan-11 95 2,180
8 Jan-12 Jan-18 88 2,268
9 Jan-19 Jan-25 102 2,370
10 Jan-26 Feb-01 130 2,500
11 Feb-02 Feb-08 311 2,811
12 Feb-09 Feb-15
13
14
15
16
17
18
19
20
21
22
=if(D16="","",E15+D16)
You dont even need the ISBLANK, just use empty quotes.
ISBLANK is what you're looking for.
In E16 =IF(ISBLANK(D16), "", E15+D16)
Use If and IsBlank in conjunction.
Do a check to see if the cell is blank, then depending on the result do the sum or the return blank. I don't have excel in front of me atm, but it should go something like this:
=if(ISBLANK(D16), "", E15+D16)
You can drag this down starting from the second cell in the column where this running total lives. The first one is obviously just the value from the adjacent cell.

Resources