Pandas DataFrame: how do we keep columns based on the index name? - python-3.x

I seem to run into some python or enumerate bugs that I am not quite sure how to fix it (See here for more details).
Long story short, I desire to see multiple data sets that has a column name of 0,4,6,8,10,12,14.
0 4 6 8 10 12
1 2 5 4 2 1
5 3 0 1 5 10
....
But my current data looks like the following
0 4 2 6 8 10 12
1 2 5 4 2 1
5 3 0 1 5 10
....
Therefore, I would like to add a code that keeps columns based on the index number (including only 0,4,6,8,10,12).
Is there a pandas function that can help with this?

Related

In Excel, is there an efficient way to sum overlapping Named Rangess?

In Microsoft Excel, I have a named (2D) range. For simplicity, let's assume it looks like this:
1
2
3
4
5
5
4
3
2
1
This represent a time series growth curve, where N number of these could kick off in any point in time. I'm looking for an efficient way to calculate what the cumulative sum of these at any point in time would be, given that N start at that point in time.
So for example, if one starts at time 0, and one at time 3, and two at time 7:
0
1
2
3
4
5
6
7
8
9
1
0
0
1
0
0
0
2
0
0
Then the cumulative total would be:
0
1
2
3
4
5
6
7
8
9
1
2
3
4
5
5
4
3
2
1
1
2
3
4
5
5
4
2
4
6
---
---
---
---
---
---
---
---
---
---
1
2
3
5
7
8
8
10
11
11
I'd like to write a formula that gets to that total without having to use those extra rows to sum over, but can't figure out how.
Use SUMPRODUCT and INDEX:
=SUMPRODUCT(INDEX($M$1:$V$1,(COLUMN()-COLUMN($A$1:A1)+1)),$A$2:A2)
The ranges are dynamic and increase as it is pulled over.
with versions that are not Office 365 we need to trick INDEX into accepting an array:
=SUMPRODUCT(INDEX($M$1:$V$1,N(IF({1},(COLUMN()-COLUMN($A$1:A1)+1)))),$A$2:A2)
This would then be confirmed with Ctrl-Shift-Enter to make it an array formula.

compare two data frames and update value in one data frame by comparing another data frame value

I have two data frames. Examples:
df1:
A B C
5 7 6
8 1 1
1 0 7
3 4 9
5 7 4
9 2 0
df2:
A B C
3 2 1
6 5 7
9 7 9
1 1 2
6 4 5
0 8 6
Both data frames have same index.
What I want is , wherever df1's value is less than 5,
I want to update df2's value to 0, else keep it same.
I tried the following code:
df2[df1<5]=0
but when I am printing df2, its showing same values as original df2.
I know I am missing something really simple.
Please help me.
Thank you.

Counting a group of columns on google spreadsheet

I have a couple of columns as shown below:
A B C D E
1 12 4 1
2 3 2 2
3 7
4 3 0 6
How would I be able to return a count of each column above so for example receive the result:
A B C D E
1 12 4 1
2 3 2 2
3 7
4 3 0 6
5 count:3 4 2 1
for each of the column. Im looking for a formula that would be able to do that in one cell(B5) returning a count for each of the columns, and avoid using fill handling as the data set is quite large
It's pretty easy, using Google Spreadsheet's functions:
=ArrayFormula(MMULT(TRANSPOSE(row(A1:A4)^0),--(len(A1:E4)>0)))
Or, if you want join them all:
=JOIN(", ",ArrayFormula(MMULT(TRANSPOSE(row(A1:A4)^0),--(len(A1:E4)>0))))

how find unique value from Different column

A B ANSWER
1 1 1
3 3 3
1 2 1
2 4 2
4 4 4
5 5 5
6 6 6
i have used this function to get above answer "=IF(ISERROR(MATCH(A2:A8,$B$1:$B$8,0)),"",A2)"
but I need answer like this i have given below (suppose if you take value in A column "1"
Which is repeated only once in column B)
A B ANR
1 1 1
3 3 3
1 2 0
2 4 2
4 4 4
5 5 5
6 6 6
I've just wrapped your formula in a condition that returns 0 where the count of the A value from start to the current row is more than one:
=IF(COUNTIF(A$1:A2,A2)>1,0,IF(ISERROR(MATCH(A2:A8,$B$1:$B$8,0)),"",A2))
.
An alternative formula that gives the same results as above for the sample data provided but may (or may not) suit the additional requirements mentioned in a comment:
=IF(COUNTIF(A$2:A$10,A2)<=COUNTIF(B$2:B$10,A2),A2,IF(COUNTIF(A$2:A2,A2)>COUNTIF(B$2:B$10,A2),0,IF(COUNTIF(A$2:A$10,A2)>COUNTIF(B$2:B2,A2),A2,0)))

Data fill in specific pattern

I am trying to fill data in MS Excel. I am given following pattern:
1 2
1
1
2 5
2 5
2
3
3 6
3
4
4
5 4
And I want my output in following format:
1 2
1 2
1 2
2 5
2 5
2 5
3 6
3 6
3 6
4
4
5 4
I tried using if(b2,b2,c1) in column 3. but that doesn't solve the problem for a=3 and a=4.
Any idea how to do this in Excel?
With sorting thus:
(the effect of which in this case is merely to move 6 up once cell) and a blank row above:
=IF(AND(A2<>A1,B2=""),"",IF(B2<>"",B2,C1))
In C2 and copied down should get the result you ask for from the data sample provided.

Resources