5

I have a dataframe in pandas

val1 val2 val3 time 
 a    b    c    0       
 d    e    f    5
 g    h    i    7
 j    k    l    4
 c    a    q    9
 m    e    t    2
 g    n    y    1
 v    k    l    0

and timesteps = [0, 3, 8]

And I want to create a new column that is the the maximal value of an elemet from timesteps that is lower than row["time"] For example, here the new column will be [0,3,3,3,8,0,0,0]

What is the best way to do so?

0

2 Answers 2

4

Using pd.cut():

timesteps = [0, 3, 8]
bins=timesteps+[df.time.max()]
#[0, 3, 8, 9]
pd.cut(df.time,bins=bins,labels=timesteps,include_lowest=True)

0    0
1    3
2    3
3    3
4    8
5    0
6    0
7    0
Sign up to request clarification or add additional context in comments.

1 Comment

Sometime I get the error "Bin edges must be unique: array([3.11141521e+09, 3.11141521e+09, 3.11141521e+09, 3.11141521e+09]" When using duplicates="drop" I get the error: Bin labels must be one fewer than the number of bin edges What should I do?
0

Try this method.

from pandas import Series,DataFrame
import pandas as pd
import random
df = DataFrame({
    'val1': ['a','d','g','j','c','m','g','v'],
    'val2':['b','e','h','k','a','e','n','k'],
    'val3':['c','f','i','l','q','t','y','l'],
    'time':[0,5,7,4,9,2,1,0]})
id = df['time'].idxmax()
max = df['time'][id]
df['timesteps'] = df.apply(lambda x: random.randint(0,max-1),axis = 1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.