I created a dict from two lists. I am trying to update values stored in the dict. I used an if statement inside a for loop to filter for rows i want to modify and now want to change the dict values for these rows. I want to change the dict values with string "Note Holder" to the corresponding family name "The {Family Name} Family. so i want to update the name(Note Holder) in the dict with new_names(The "blank" Family). Here is my code:
data = pd.read_excel('mailclean.xlsx')
ran = range(len(data))
tes = list(data.iloc[0:len(data)]['Column1'])
tes1 = list(data.iloc[0:len(data)]['Column2'])
dic = {}
for a in ran:
dic[tes[a]] = tes1[a]
for new_name in tes:
if "Family Trust" in new_name:
name = dic.get(new_name)
new_name = "The " new_name[:-6]
print(new_name)
print(name)
Here is the terminal output:
The Guerland Family
Note Holder
The Sciammas Family
Note Holder
The Hsu Family
Note Holder
The Hemingway Family
Note Holder
The Williams Family
Note Holder
The Svancara Family
Note Holder
The Schritter Family
Note Holder
The Meola Family
Note Holder
The Scipione Family
Note Holder
The Elmore Family
Note Holder
The Oeland Family
Note Holder
The Harmsen Family
Note Holder
The Mauger Family
Note Holder
The Hockema Family
Note Holder
The Vogl Family
Note Holder
The McKee Family
Note Holder
The Davis Family
Note Holder
The Katz Family
Note Holder
The Grewal Family
Note Holder
The Penner Family
Note Holder
The Johnson Family
Note Holder
The Johnson Family
Note Holder
The Johnson Family
Note Holder
The Nemeth Family
Note Holder
The Horan Family
Note Holder
The Quan Family
Note Holder
The Elliott Family
Note Holder
The Clay Family
Note Holder
The Edwards Family
Note Holder
The Smith Family
Note Holder
The Rau Family
Note Holder
The Barter Family
Note Holder
The Lyon Family
Note Holder
The Scheiber Family
Note Holder
The Betzing Family
Note Holder
The Porter Family
Note Holder
The Eller Family
Note Holder
The Sebring Family
Note Holder
I want to change the dict values with string "Note Holder" to the corresponding family name "The {Family Name} Family. so i want to update the name(Note Holder) in the dict with new_names(The "blank" Family).
CodePudding user response:
If I understand you correctly, you first created a dictionary where the keys are elements in the array tes
and the values are tes1
in the corresponding array index.
You then want to, given a certain condition (your if statement), change the value (which is from tes1
) from Note Holder
to The {Family Name} Family
?
Perhaps something like:
for name in tes:
if "Family Trust" in name:
new_name = "The " name[:-6]
dict[new_name] = new_name
You can directly update the dictionary values like this, given I understand you question.
CodePudding user response:
OK, so as written, you're throwing away vital information.
for new_name in tes:
if "Family Trust" in new_name:
name = dic.get(new_name)
new_name = "The " new_name[:-6]
print(new_name)
print(name)
The new_name
variable is the only anchor you have to where you are in the dictionary, and you're changing it to something else, thereby losing your place.
Changing the variable holding the key doesn't make any sense. You want to change the value stored at that key, which is just another assignment exactly like the one you used to put the value there in the first place:
for name in dic:
if "Family Trust" in name:
dic[name] = "The " name[:-6]
In fact, you could have done it in the first place by modifying the original loop that created the dict:
for a in ran:
if "Family Trust" in tes[a]:
dic[tes[a]] = "The " tes[a][:-6]
else:
dic[tes[a]] = tes1[a]