Python 双forloop(通过数据帧和列表)

Python 双forloop(通过数据帧和列表),python,dataframe,for-loop,nan,Python,Dataframe,For Loop,Nan,我有一个test2数据帧: manufacturer condition fuel drive cylinders description 0 ford excellent gas rwd NaN ford in excellent condition. 4 cylinders 1 cadillac NaN NaN NaN 4 cyli

我有一个test2数据帧:

   manufacturer  condition   fuel        drive  cylinders       description
0   ford        excellent    gas          rwd    NaN            ford in excellent condition. 4 cylinders
1   cadillac    NaN          NaN          NaN    4 cylinders    4 cylinders. Half-new cadillac. Diesel.
2   NaN         new          diesel       fwd    12 cylinders   Ford, diesel, new condition.
3   NaN         NaN          electric     NaN    10 cylinders   Ferrari, excellent condition. 4wd
4   ferrari     NaN          NaN          4wd    NaN            New ferrari. Electric with 12 cylinders.
我想循环数据帧,并使用列“description”的信息填充每列的NaN值。为此,我做了以下工作:

import re

manufacturer = '(ford | cadillac | ferrari)'
condition = '(excellent, good, fair, like new, salvage, new)'
fuel = '(gas, hybrid, diesel, electric)'
drive = '(\S*wd)'
cylinders = '(\d+\s+cylinders?)'

test2['manufacturer'] = test2['manufacturer'].fillna(
    test2['description'].str.extract(manufacturer, flags=re.IGNORECASE, expand=False)).str.lower()
test2['condition'] = test2['condition'].fillna(
    test2['description'].str.extract(condition, flags=re.IGNORECASE, expand=False)).str.lower()
test2['fuel'] = test2['fuel'].fillna(
    test2['description'].str.extract(fuel, flags=re.IGNORECASE, expand=False)).str.lower()
test2['drive'] = test2['drive'].fillna(
    test2['description'].str.extract(drive, flags=re.IGNORECASE, expand=False)).str.lower()
test2['cylinders'] = test2['cylinders'].fillna(
    test2['description'].str.extract(cylinders, flags=re.IGNORECASE, expand=False)).str.lower()

test2
但它看起来不太好,所以我尝试使用for循环来简化编程:

columns = [manufacturer, condition, fuel, drive, cylinders]

for i in test2:
   for column in columns:
      test2[i] = test2[i].fillna(
        test2['description'].str.extract(column, flags=re.IGNORECASE, expand=False)).str.lower()
不管我怎么努力,它总是给我错误。它在test2中的“i”上循环得很好,但是当它开始在列表“columns”上循环时,循环会出错

你知道我该怎么解决这个问题吗?
谢谢大家!

每个元素循环多次。每个元素只能循环一次。使用
zip
功能`合并键和列表

请尝试以下代码:

keys =    ['manufacturer', 'condition', 'fuel', 'drive', 'cylinders']
columns = [ manufacturer,   condition,   fuel,   drive,   cylinders]

for i,column in zip(keys,columns):
   test2[i] = test2[i].fillna(
      test2['description'].str.extract(column, flags=re.IGNORECASE, expand=False)).str.lower()

该列表应称为
,而不是
啊,对不起,它称为列。。。