This question already has an answer here:
Compute the running (cumulative) maximum for a series in pandas
(1 answer)
Closed 1 year ago.
Lets say I get a list from a dataframe, and the list goes
list = [1, 3, 2, 6, 4, 2, 7, 4, 2, 6, 8]
I want to get returned a dataframe that plots the highest recognized value. ex:
list2= [1, 3, 3, 6, 6, 6, 7, 7, 7, 7, 8]
As shown in the example above, a new list is generated. plots the highest value found.
I need it returned as its own column in the dataframe file.
My code for referrence;
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
# read data frame
df = pd.read_csv('file.csv')
# numbers list: [1, 3, 2, 6, 4, 2, 7, 4, 2, 6, 8]
df['numbers']
# here will go the code you guys give me ###
df['highestnumbers'] = #####################
the output should be a list that goes
[1, 3, 3, 6, 6, 6, 7, 7, 7, 7, 8]
df['highestnumbers'] = df['numbers'].expanding().max()
Related
This question already has answers here:
Vectorized lookup on a pandas dataframe
(3 answers)
Closed 3 years ago.
I have a pandas DataFrame with values in a number of columns, make it two for simplicity, and a column of column names I want to use to pick values from the other columns:
import pandas as pd
import numpy as np
np.random.seed(1337)
df = pd.DataFrame(
{"a": np.arange(10), "b": 10 - np.arange(10), "c": np.random.choice(["a", "b"], 10)}
)
which gives
> df['c']
0 b
1 b
2 a
3 a
4 b
5 b
6 b
7 a
8 a
9 a
Name: c, dtype: object
That is, I want the first and second elements to be picked from column b, the third from a and so on.
This works:
def pick_vals_from_cols(df, col_selector):
condlist = np.row_stack(col_selector.map(lambda x: x == df.columns))
values = np.select(condlist.transpose(), df.values.transpose())
return values
> pick_vals_from_cols(df, df["c"])
array([10, 9, 2, 3, 6, 5, 4, 7, 8, 9], dtype=object)
But it just feels so fragile and clunky. Is there a better way to do this?
lookup
df.lookup(df.index, df.c)
array([10, 9, 2, 3, 6, 5, 4, 7, 8, 9])
Comprehension
But why when you have lookup?
[df.at[t] for t in df.c.items()]
[10, 9, 2, 3, 6, 5, 4, 7, 8, 9]
Bonus Hack
Not intended for actual use
[*map(df.at.__getitem__, zip(df.index, df.c))]
[10, 9, 2, 3, 6, 5, 4, 7, 8, 9]
Because df.get_value is deprecated
[*map(df.get_value, df.index, df.c)]
FutureWarning: get_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead
[10, 9, 2, 3, 6, 5, 4, 7, 8, 9]
This question already has answers here:
How to delete an element from a list while iterating over it in Python? [duplicate]
(2 answers)
Closed 5 years ago.
I want to know how to append all the items that weren't removed into a new list.
challenge = [1, 0, 9, 8, 5, 4, 1, 9, 3, 2, 3, 5, 6, 9]
def remove_values(thelist, value):
newlist = []
while value in thelist:
thelist.remove(value)
newlist.append()
bye = remove_values(challenge, max(challenge))
For example, if I remove all the 9s (the max), how do I append the rest into a new list?
challenge = [1, 0, 9, 8, 5, 4, 1, 9, 3, 2, 3, 5, 6, 9]
# This will append every item to a new List where the value not is max
# You won't need 2 lists to achieve what you want, it can be done with a simple list comprehension
removed_list = [x for x in challenge if x != max(challenge)]
print(removed_list)
# will print [1, 0, 8, 5, 4, 1, 3, 2, 3, 5, 6]
This question already has answers here:
Efficient way to rotate a list in python
(27 answers)
Closed 6 years ago.
In detail, ex: I have a list weekday:
list_week_day = [0,2,4,5,6]
if today.weekday() = 3 , then order list_week_day = [4,5,6,0,2]
So, how to do that ???
new = old[n:] + old[:n]
You append the front part of the list to the back part. Can you finish after that hint? n is your weekday.
Could also try:
wday = 3
[(x + wday) % 7 for x in list_week_day]
# [3, 4, 5, 6, 0, 1, 2]
Did you try the following? I suspect it is not the most efficient way to get what you desire, but it certainly is a way to get it.
list_week_day[today.weekday() : ] + list_week_day[ : today.weekday()]
As suggested here, you can use numpy's roll command, choosing a suitable value to roll by:
>>> import numpy
>>> a=numpy.arange(1,10) #Generate some data
>>> numpy.roll(a,1)
array([9, 1, 2, 3, 4, 5, 6, 7, 8])
>>> numpy.roll(a,-1)
array([2, 3, 4, 5, 6, 7, 8, 9, 1])
>>> numpy.roll(a,5)
array([5, 6, 7, 8, 9, 1, 2, 3, 4])
>>> numpy.roll(a,9)
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
Considering "b" defined below as a list of dictionaries. How can I remove element 6 from the 'index' in second element of b (b[1]['index'][6]) and save the new list to b?
import pandas as pd
import numpy as np
a = pd.DataFrame(np.random.randn(10))
b = [{'color':'red','index':a.index},{'color':'blue','index':a.index}]
output:
[{'color': 'red', 'index': Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')}, {'color': 'blue', 'index': Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64')}]
I tried np.delete and .pop or .del for lists (no success), but I do not know what is the best way to do it?
I think this will work for you
import pandas as pd
import numpy as np
a = pd.DataFrame(np.random.randn(10))
print a
b = [{'color':'red','index':a.index},{'color':'blue','index':a.index}]
d = b[1]['index']
b[1]['index'] = d.delete(6)
print b[1]['index']
Int64Index([0, 1, 2, 3, 4, 5, 7, 8, 9], dtype='int64')
This question already has answers here:
Sorting list based on values from another list
(20 answers)
Closed 9 years ago.
I have a list of objects and I'd like to sort them based on a parallel array. So, as I operate over a list of data I construct a parallel array (where each entry in that list corresponds to an entry in the original list). Then (let's say the parallel array is filled with numbers)
list_a = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
list_b = (4, 2, 5, 6, 1, 7, 3, 9, 0, 8 )
I want to sort the original list of objects based on the parallel arrays values so that the original list is sorting in ascending order by the numerical value in the other array. Is there any way to do this built into python?
sort_a_by_b(list_a, list_b)
Expected result would be:
list_a_sorted_by_b = (8, 4, 1, 6, 0, 2, 3, 5, 9, 7 )
>>> list_a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list_b = [4, 2, 5, 6, 1, 7, 3, 9, 0, 8]
>>>
>>> import operator
>>>
>>> [k for k, v in sorted(zip(list_a, list_b), key=operator.itemgetter(1))]
[8, 4, 1, 6, 0, 2, 3, 5, 9, 7]
Call the object list objects and the other list sort_keys. If you can compute sort_keys[i] from just the value of objects[i], you don't even need to build sort_keys. You should just do this:
objects.sort(key=compute_sort_key_for_object)
where compute_sort_key_for_object is the function you would use to compute sort_keys[i] from objects[i]. It's faster and more readable.
If the processing to compute sort_keys is more complex, you'll want Rohit's answer:
import operator
[k for k, v in sorted(zip(objects, sort_keys), key=operator.itemgetter(1))]