Skip to content Skip to sidebar Skip to footer

Insert Value Based On Row Index Number In A Pandas Dataframe

I need to insert value into a column based on row index of a pandas dataframe. import pandas as pd df=pd.DataFrame(np.random.randint(0,100,size=(11, 4)), columns=list('ABCD')) df['

Solution 1:

I like to use np.select for this kind of task, because I find the syntax intuitive and readable:

# Set up your conditions:
conds = [(df.index >= 0) & (df.index <= row_25),
         (df.index > row_25) & (df.index<=row_50),
         (df.index > row_50) & (df.index<=row_75),
         (df.index > row_75)]

# Set up your target values (in the same order as your conditions)
choices = ['$', '$$', '$$$', '$$$$']

# Assign df['ticker']df['ticker'] = np.select(conds, choices)

returns this:

>>> df
     A   B   C   D ticker
092972579$
17642694$
249651991$
37638345$$48316016$$51569744$$678171886$$$
755568391$$$
876165233$$$
955358095$$$$1090294187$$$$

Solution 2:

I think cut can solve this problem

df['ticker']=pd.cut(np.arange(len(df))/len(df), [-np.inf,0.25,0.5,0.75,1], labels=["$","$$",'$$$','$$$$'],right=True)
df
Out[35]: 
     A   B   C   D ticker
063511933$
11280571$
253276226$
397433180$$491229211$$539708226$$632621775$$$
75597972$$$
8754474$$$
94354566$$$$102997494$$$$

Solution 3:

You can set up a few np.where statements to handle this. Try something like the following:

import numpy as np
...
df['ticker'] = np.where(df.index < row_25, "$", df['ticker'])
df['ticker'] = np.where(row_25 <= df.index < row_50, "$$", df['ticker'])
df['ticker'] = np.where(row_50 <= df.index < row_75, "$$$", df['ticker'])
df['ticker'] = np.where(row_75 <= df.index, "$$$$", df['ticker'])

Solution 4:

This is one explicit solution using .loc accessor.

import pandas as pd

df = pd.DataFrame(np.random.randint(0,100,size=(11, 4)), columns=list('ABCD'))
n = len(df.index)

df['ticker'] = 'na'
df.loc[df.index <= n/4, 'ticker'] = '$'
df.loc[(n/4 < df.index) & (df.index <= n/2), 'ticker'] = '$$'
df.loc[(n/2 < df.index) & (df.index <= n*3/4), 'ticker'] = '$$$'
df.loc[df.index > n*3/4, 'ticker'] = '$$$$'#      A   B   C   D ticker# 0   47  64   7  46      $# 1   53  55  75   3      $# 2   93  95  28  47      $# 3   35  88  16   7     $$# 4   99  66  88  84     $$# 5   75   2  72  90     $$# 6    6  53  36  92    $$$# 7   83  58  54  67    $$$# 8   49  83  46  54    $$$# 9   69   9  96  73   $$$$# 10  84  42  11  83   $$$$

Post a Comment for "Insert Value Based On Row Index Number In A Pandas Dataframe"