I have two lists:
single = ['A','B']
double = ['AA','BB']
Data stored in dataframe df:
0 1 2 3
0 All 1 AA Yes
1 A 2 All No
where All means ['A','B'] in column 0 and means ['AA','BB'] in column 2, I want to obtain the following dataframe df2
0 1 2 3
0 A 1 AA Yes
1 B 1 AA Yes
2 A 2 AA No
3 A 2 BB No
and the order of the row index doesn't matter. I am now doing:
single = ['A','B']
double = ['AA','BB']
df=pd.DataFrame([['All',1,'AA','Yes'],['A',2,'All','No']])
index = []
for i in range(len(df)):
if df.loc[i,0] == 'All':
index.append(i)
for j in single:
df.loc[len(df),:] = df.loc[i,:]
df.loc[len(df)-1,0] = j
df = df.drop(index).reset_index(drop=True)
index = []
for i in range(len(df)):
if df.loc[i,2] == 'All':
index.append(i)
for j in double:
df.loc[len(df),:] = df.loc[i,:]
df.loc[len(df)-1,2] = j
df2 = df.drop(index).reset_index(drop=True)
print df2
It first adds two rows to represent 'All' in column 0 and delete this row. Then for the 'All' in column 2.
Any easier way to do this 'find and replace'?