If I have a DataFrame that stores values from 2 columns (A & B) from a CSV file, how would I populate a dictionary using a for loop to get the values from the DataFrame?
I need to store the rows of A B pairs like this.. A_&_B_sets = {1:[A1,B1],2:[A2,B2],…}
A_&_B_sets = {1:[A1,B1],2:[A2,B2],…}
for i in (1,n 1):
A_&_B_sets[i] = i * I
I am quite lost. Any help greatly appreciated.
CodePudding user response:
If you have df
like this:
Column A Column B
0 1 4
1 2 5
2 3 6
Then you can do:
A_and_B_sets = {}
for i, (_, row) in enumerate(df.iterrows(), 1):
A_and_B_sets[i] = [row["Column A"], row["Column B"]]
print(A_and_B_sets)
to print:
{1: [1, 4], 2: [2, 5], 3: [3, 6]}