Home > Back-end >  Adding new column based on index list in pandas
Adding new column based on index list in pandas

Time:07-27

I have a pandas dataframe like,

pd.DataFrame({'f1':[23,56,7, 56,34, 98],
              'f2':[32,85,27, 36,64, 60]})

    f1  f2
0   23  32
1   56  85
2   7   27
3   56  36
4   34  64
5   98  60

and based on an index list like index_list = [2, 4] I want to add a new column to original datafram like following,

    new_column  f1  f2
0      0        23  32
1      0        56  85
2      0        7   27
3      1        56  36
4      1        34  64
5      2        98  60

Note: index list shows the locations that new_column should increase 1 integer up.

CodePudding user response:

# Put a 1 after the index of each index in your list as a new column.
df.loc[[x 1 for x in index_list], 'new_column'] = 1

# fill with 0's, and take the cumulative sum.
df.new_column = df.new_column.fillna(0).cumsum()

print(df)

Output:

   f1  f2  new_column
0  23  32         0.0
1  56  85         0.0
2   7  27         0.0
3  56  36         1.0
4  34  64         1.0
5  98  60         2.0

If your index list is actually an index:

# If index_list looks like:
>>> index_list
Int64Index([2, 4], dtype='int64')

# Then you can do:
df.loc[index_list 1, 'new_column'] = 1
...

CodePudding user response:

A simple way is to use cumsum:

df = pd.DataFrame(index=range(6))
index_list = [2, 4]
index_list = [x 1 for x in index_list]
df["new"] = 0
df["new"].loc[index_list] = 1
df["new"].cumsum()

which gives:

0    0
1    0
2    0
3    1
4    1
5    2

CodePudding user response:

Here's a way to get the exact output specified in your question:

df2 = df.reindex(pd.Series(index_list) 1)
df = df.assign(new_column=pd.Series(range(1, 1 len(df2)), index=df2.index))[['new_column']   list(df.columns)]
df.new_column = df.new_column.ffill().fillna(0).astype(int)

Output:

   new_column  f1  f2
0           0  23  32
1           0  56  85
2           0   7  27
3           1  56  36
4           1  34  64
5           2  98  60
  • Related