I have a list and a dataframe. f.e.:
List1=[1, 4, 6, 15]
DataFrame:
A B C
Daisy 5 -1
Sunflower 3 5
Tulip 15 5
Orchid 8 6
Lotus 23 -1
Poppy 9 6
Lily 40 -1
I want to replace the values of B, where C>0 with the values of the List1. So it would look like this:
A B C D
Daisy 5 -1 5
Sunflower 3 8 1
Tulip 15 5 4
Orchid 8 7 6
Lotus 23 -1 23
Poppy 9 6 15
Lily 40 -1 40
I tried this code using pandas and sqlite3:
Table1 = pd.read_excel(Flowers)
Table1["id2"]=range(1, len(Table1)+1)
List1=[1, 4, 6, 15]
indexes=[]
conn = sqlite3.connect('Table1.db')
crsr = conn.cursor()
conn.commit()
Table1.to_sql('Table1', conn, if_exists='replace', index= False)
crsr.execute("SELECT id2 FROM Table1 WHERE C BETWEEN 0 and 20")
for row in crsr.fetchall():
indexes.extend(row)
e=0
for i in indexes:
Table1.loc[i-1,Table1.C[i-1]] = List1[e]
if e+1>len(List1):
break
else:
e=e+1
print(Table1)
And get this result:
A B C id2 8 5 7 6
0 Daisy 5 -1 1 NaN NaN NaN NaN
1 Sunflower 3 8 2 1.0 NaN NaN NaN
2 Tulip 15 5 3 NaN 4.0 NaN NaN
3 Orchid 8 7 4 NaN NaN 6.0 NaN
4 Lotus 23 -1 5 NaN NaN NaN NaN
5 Poppy 9 6 6 NaN NaN NaN 15.0
6 Lily 40 -1 7 NaN NaN NaN NaN
How can I replace the values in column B with the List elements? Thank you for the help!