[英]python for loop mapping into dataframe
I have two lists: 我有两个清单:
number = [21, 44]
access = ["denied", "Try Again"]
I combined these two variable into one object: 我将这两个变量合并为一个对象:
testInput = [number, access]
output of testInput
: testInput
输出:
[[21, 44], ['denied', 'Try Again']]
Now I want to for loop through testInput
and extract the values and map them into a key inside of a data frame. 现在,我想for循环通过
testInput
并提取值并将它们映射到数据框内的键中。
Here is what I tried: 这是我尝试过的:
for number, access in testInput:
df = df.append({'Access Message': access, 'Number': number},ignore_index=True)
print df
Output of df: df的输出:
Access Message Number
0 44 21
1 Try Again denied
The problem that I am facing is the value of number (44) is returning as a value of access when it shouldn't. 我面临的问题是数字(44)的值在不应该作为访问的值返回时。
When I 'print number' inside the for loop I get 当我在for循环中“打印数字”时,我得到
21
denied
What I ultimately want in df
as an output of the for loop is 我最终想要在
df
作为for循环的输出是
Access Message Number
0 denied 21
1 Try Again 44
Am I doing something wrong in my for loop? 我在for循环中做错什么了吗? or could I have done something before the for loop differently?
还是可以在for循环之前做一些不同的事情?
You can just use 你可以用
df = pd.DataFrame({'Access Message': access, 'Number': number})
which yields the desired outcome 产生预期的结果
Access Message Number
0 denied 21
1 Try Again 44
If you actually want to append to an existing dataframe as @Tim mentioned in the comments, you can do 如果您实际上想像注释中提到的@Tim一样附加到现有数据框,则可以执行
for ni, aci in zip(number, access):
df = df.append({'Access Message': aci, 'Number': ni}, ignore_index=True)
which gives (I just appended to df
which I created above) 给出(我刚刚附加到上面创建的
df
中)
Access Message Number
0 denied 21
1 Try Again 44
2 denied 21
3 Try Again 44
But I guess what would be more efficient for huge lists is to use concat
(as also @Wen suggests in the comments): 但是我猜想对于大型列表更有效的方法是使用
concat
(正如@Wen在评论中建议的那样):
append_me = pd.DataFrame(zip(number, access)).rename(columns={0: 'Number', 1: 'Access Message'})
df = pd.concat([df, append_me])
which gives (I again just appended to the previous dataframe) 给出(我再次只是附加到上一个数据框)
Access Message Number
0 denied 21
1 Try Again 44
2 denied 21
3 Try Again 44
0 denied 21
1 Try Again 44
You are iterating using two values: for num, access in...
but your testInput is not separated that way. 您正在使用两个值进行迭代:
for num, access in...
但您的testInput并非以这种方式分开。 All the numbers are in the first item and all the access msgs are in the second item. 所有数字都在第一项中,所有访问消息都在第二项中。 what you want is a list of number-access _pairs .
您想要的是number-access _pairs列表。 EG
例如
[(21, 'denied'), (44, 'Try Again'), (99, 'someMessage') ...]
try 尝试
pairs = zip(testInput[0], testInput[1])
this takes the first item in each list ( testInput[0]
and testInput[1]
) and matches them together. 这将获取每个列表中的第一项(
testInput[0]
和testInput[1]
)并将它们匹配在一起。 So the first number gets put into a tuple with the first access, the second num with second access... and so on. 因此,第一个数字将被放入具有第一个访问权限的元组中,第二个数字将具有第二个访问权限...等等。
Then run pairs
through for loop 然后运行
pairs
循环
for number, access in pairs:
df = df.append({'Access Message': access, 'Number': number},ignore_index=True)
print df
Instead of having: 而不是:
testInput = [number, access]
try: 尝试:
testInput = zip(number, access)
The zip()
method creates pairs by interleaving the two lists. zip()
方法通过交织两个列表来创建对。 So, it would give 所以,它会给
[(21, 'denied'), (44, 'Try Again')]
which should fit nicely with what you're trying to do. 这应该很好地适合您要尝试做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.