'How to append value in column with python pandas
I am trying to append new values in column with pandas. This is data frame example: code:
data = pd.read_csv('data/email.csv', sep=";")
data.insert(0,'imgid','')
print(data)
result:
imgid email id fname lname
0 [email protected] 2070 Laura Grey
1 [email protected] 4081 Craig Johnson
2 [email protected] 9346 Mary Jenkins
3 [email protected] 5079 Jamie Smith
And then I have this code:
import os
for root, dirs, files in os.walk("data/"):
for file in files:
if file.endswith(".jpg"):
img = file.split('.')[0]
print(file, " is ", img)
result:
2070.jpg is 2070
4081.jpg is 4081
5079.jpg is 5079
9346.jpg is 9346
As a final result I want to compare data['id'] to img and if they are equal, add img's full file name in data["imgid"].
Is there any simple way to do that , I mean not to load memory too much. Thanks
Solution 1:[1]
You can try this:
import os
lst = []
for root, dirs, files in os.walk("data/"):
for file in files:
if file.endswith(".jpg"):
img = file.split('.')[0]
lst.append([img,file])
np_arr = np.array(lst)
bool_arr = (df["num_wings"] == np_arr[:,0])
data.loc[bool_arr,"imgid"] = np_arr[bool_arr,1]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Phoenix |
