What is a better way to create the same column mentioned below:
col_new = []
for r1 in df['col_A']:
if r1==1:
for r2 in df['col_B']:
if r2!='None':
col_new.append('col_new')
df['col_new'] = col_new
My dataframe is huge (120k * 22) and running the above code is hanging the notebook. Is there a faster and more efficient way to create this column where it represents all the non-null values of col_B when col_A is 1.
I believe need to create boolean mask and then append value by DataFrame.loc
:
mask = (df['col_A'] == 1) & (df['col_B']!='None')
#if None is not string
#mask = (df['col_A'] == 1) & (df['col_B'].notnull())
df.loc[mask, 'col_new'] = 'col_new'
Sample :
In column are strings None
s:
df = pd.DataFrame({
'col_A': [1,1,2,1],
'col_B': ['a','None','None','a']
})
print (df)
col_A col_B
0 1 a
1 1 None
2 2 None
3 1 a
mask = (df['col_A'] == 1) & (df['col_B']!='None')
df.loc[mask, 'col_new'] = 'val'
print (df)
col_A col_B col_new
0 1 a val
1 1 None NaN
2 2 None NaN
3 1 a val
In column are not strings None
s , then use Series.notna
:
df = pd.DataFrame({
'col_A': [1,1,2,1],
'col_B': ['a',None,None,'a']
})
print (df)
col_A col_B
0 1 a
1 1 None
2 2 None
3 1 a
mask = (df['col_A'] == 1) & (df['col_B'].notna())
#oldier pandas versions
#mask = (df['col_A'] == 1) & (df['col_B'].notnull())
df.loc[mask, 'col_new'] = 'val'
print (df)
col_A col_B col_new
0 1 a val
1 1 None NaN
2 2 None NaN
3 1 a val
Also if want use if-else
statement numpy.where
is really helpfull:
df['col_new'] = np.where(mask, 'val', 'another_val')
print (df)
col_A col_B col_new
0 1 a val
1 1 None another_val
2 2 None another_val
3 1 a val
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.