简体   繁体   中英

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 :

[[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.

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:

  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.

When I 'print number' inside the for loop I get

21
denied

What I ultimately want in df as an output of the for loop is

  Access Message  Number
0         denied      21
1      Try Again      44

Am I doing something wrong in my for loop? or could I have done something before the for loop differently?

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

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)

  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):

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. 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 . 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. 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

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. So, it would give

 [(21, 'denied'), (44, 'Try Again')]

which should fit nicely with what you're trying to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM