Making column that aggregates the values of another column w/ Python [duplicate] - python

This question already has an answer here:
Cumsum as a new column in an existing Pandas data
(1 answer)
Closed 2 years ago.
Let sat I have a DataFrame with column A.
A= (1,2,3,4,5,6...n)
I want to create column B like this:
B=(1,3,6,10,15,21...n)
Explicitly: i+(sum of all the previous numbers)
Probably simple, but hard for me:P Very new to programming
Thanks!

from itertools import accumulate
A = [1, 2, 3, 4, 5, 6]
B = list(accumulate(A)) #->[1, 3, 6, 10, 15, 21]

Related

Seaborn split column names for hue [duplicate]

This question already has answers here:
dataframe to long format
(2 answers)
Reshape wide to long in pandas
(2 answers)
Split pandas column and add last element to a new column
(2 answers)
Get last "column" after .str.split() operation on column in pandas DataFrame
(5 answers)
Closed 8 months ago.
I have a dataframe like this.
df = pd.DataFrame(np.array([[1, 2, 3, 4], [4, 5, 6, 4], [7, 8, 9, 4]]),
columns=['NP_A', 'NP_B', 'NP_C', "OP_A"])
I would like to use sns.lineplot.
But instead of the name of the columns as 'hue' I would like to make a split of the names.
"NP", "A"
"NP", "B"
...
In order to have a legend that considers the two combinations.

Find nearest values in numpy array [duplicate]

This question already has answers here:
Find elements of array one nearest to elements of array two
(1 answer)
Find nearest indices for one array against all values in another array - Python / NumPy
(2 answers)
Closed 3 years ago.
Is there a numpy-thonic way to get the nearest values between two arrays:
Example:
a = np.array([1, 2, 3, 4, 5, 6, 7, 8])
b = np.array([1.1, 3.2, 4.9, 7.2])
c = find_nearests(a, b)
c would be the size of b with the nearest elements from a:
print(c)
([1, 3, 5, 7])
This question is a direct reference to the following one that applies for searching one element in a tab: Find nearest value in numpy array

Get ascending order of numbers in array in python [duplicate]

This question already has answers here:
Translate integers in a numpy array to a contiguous range 0...n
(2 answers)
Closed 4 years ago.
I have a Numpy array with some numbers and I would like to get order the items ascending order.
For example, I have a list:
[4, 25, 100, 4, 50]
And I would like to use a function to get this:
[1, 2, 4, 1, 3]
Any ideas how to do this?
There is a convenient method via pandas:
import pandas as pd
lst = [4, 25, 100, 4, 50]
res = pd.factorize(lst, sort=True)[0] + 1
# [1 2 4 1 3]

iPython 3.5.2 lists [duplicate]

This question already has answers here:
Python - How to extract the last x elements from a list [duplicate]
(4 answers)
Closed 6 years ago.
I have created a list size 12, ex: V[0 1 2 3 4 5 6 7 8 9 10 11]
How do I return only the last 10 digits [2:11]? Also what if the list had n variables, I tried V[2,:], but I get TypeError: list indices must be integers or slices, not tuple.
If you want to get the last x elements of a list, you need to use a negative index in your slice.
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers[-5:]
[5, 6, 7, 8, 9]
As for the error you mentioned, it looks like you have a stray comma after the 2, which makes 2 the first element in a tuple. Take out the comma and it should be a valid slice.

How to delete repeated elements from array keeping the order unchanged in python? [duplicate]

This question already has answers here:
numpy.unique with order preserved
(7 answers)
Closed 6 years ago.
I have sequence of integer elements in array "a" ,given below
a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
I need the output like
output=[2,1,5,4,8,6,87,62,3]
I tried the built-in functions like set or unique but it arrange the
resulting sequence in ascending order, I want to keep the order unchanged.
Can anyone help?
You can use sorted with key=list.index
>>> a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
>>> new_a = sorted(set(a), key=a.index)
>>> new_a
[2, 1, 5, 4, 8, 6, 87, 62, 3]
a=[2,1,5,4,8,4,2,1,2,4,8,6,1,5,4,87,62,3]
b = []
You can use
[b.append(x) for x in a if x not in b]
or easier to read
for x in a:
if x not in b:
b.append(x)
>>> [2, 1, 5, 4, 8, 6, 87, 62, 3]`

Categories