I would like to add a new column to my orignal dataframe where my list values == dataframe index add 1 else 0. However, my list isn't the same size... I tried with a for loop but its too slow.. and wanted to either do it with a list comp or vectorized ( or any other way thats quick).. any ideas on how i do it
My Data:
list =[501,205,432,....]
my code:
df = df.reset_index(drop=True)
for r in range(0,len(df)):
for peak in peaks:
if df.index[r]==peak:
df.loc[r,"peaks"]=1
else:
df.loc[r,"peaks"]=0
Try using isin on index:
import pandas as pd
df = pd.DataFrame({'a': {100: 1, 200: 2, 300: 3, 400: 4, 500: 5}})
peaks = [300, 400, 500]
df['peaks'] = df.index.isin(peaks).astype(int)
print(df)
Vectorized via numpy
import pandas as pd
import numpy as np
df = pd.DataFrame({'a': {100: 1, 200: 2, 300: 3, 400: 4, 500: 5}})
peaks = [300, 400, 500]
df['peaks'] = np.where(df.index.isin(peaks), 1, 0)
print(df)
Output:
a peaks
100 1 0
200 2 0
300 3 1
400 4 1
500 5 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.