[英]Populating a dictionary of lists with values from 2 columns of a DataFrame
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?如果我有一个 DataFrame 存储来自 CSV 文件的 2 列(A 和 B)的值,我将如何使用 for 循环填充字典以从 DataFrame 中获取值?
I need to store the rows of AB pairs like this.. A_&_B_sets = {1:[A1,B1],2:[A2,B2],…}我需要像这样存储 AB 对的行.. 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.
非常感谢任何帮助。
If you have df
like this:如果你有这样的
df
:
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]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.