openpyxl数据帧过滤

时间:2020-06-22 23:28:34

标签: python dataframe openpyxl

我刚刚开始使用python,在用各自的值过滤F和I列之后,需要将K列中的所有内容都放入列表中。

因此,基本上,当F列匹配stringA且I列匹配stringC时,则将K列的所有值保存到列表中。我已经将我的代码用于导入正确的模块,打开和保存工作表,而我只需要对此部分的帮助。

我敢肯定有另一种方法可以实现它。

l = []
for icol in sheet1.columns:
    coll = icol[0].column
    for cell in icol:
        if(coll == 'F' and cell.value == 'stringA' or coll == 'I' and cell.value == 'stringC'):
            print(coll, cell.value)
            if (coll == 'K'):
                l.append(cell.value)
print(l)

我真正需要的是在附加行中指定单元格名称。也许有一种非常蟒蛇的方式来做到这一点。我会分享的。

1 个答案:

答案 0 :(得分:0)

假设您已经安装了pandasxlrdopenpyxl,则可以使用:

import pandas as pd

# this example data should result in a list with only 'value 1' and 'value 6'
df = pd.DataFrame([
    [None, None, None, None, None, 'stringA', None, None, 'stringC', None, 'value 1'],
    [None, None, None, None, None, 'stringX', None, None, 'stringC', None, 'value 2'],
    [None, None, None, None, None, 'stringA', None, None, 'stringX', None, 'value 3'],
    [None, None, None, None, None, None     , None, None, 'stringC', None, 'value 4'],
    [None, None, None, None, None, 'stringA', None, None, None     , None, 'value 5'],
    [None, None, None, None, None, 'stringA', None, None, 'stringC', None, 'value 6'],
])

# just writing the file, so you can verify it matches your input data
df.to_excel('test.xlsx', header=False, index=False)

# As @JiWei suggests, but using the column index instead of the name
print(df[(df[5] == 'stringA') & (df[8] == 'stringC')][10].tolist())

结果:

['value 1', 'value 6']

因此,如果您已经拥有test.xlsx之类的文件,则只需:

import pandas as pd

df = pd.read_excel('test.xlsx', header=None)
print(df[(df[5] == 'stringA') & (df[8] == 'stringC')][10].tolist())