Python for循环无效语法(替换数据框中列的内容)

时间:2020-01-21 17:08:48

标签: python pandas

很抱歉这个简单的问题,但是我在这里迷失了方向。通过研究先前的类似问题,似乎我必须先看一下这一行才能更正此问题。但是,我之前的代码很好,实际上添加for循环会引入错误。

基本上,我想循环数据框列表,并将第一列(列名= BOL)的内容替换为值BLS。 (数据框1列获得BLS 1,数据框2列获得BLS 2,依此类推...)

下面是错误和代码。

文件“ ex2.py”,第41行 用于BLS中的x ^ SyntaxError:语法无效

import tkinter as tk
from tkinter import filedialog
from pathlib import Path
import pandas as pd

def df_style(val):
              return 'font-weight: bold'

root = tk.Tk()
root.withdraw()

BLS = list(map(str, input("Please enter the BL#'s: ").split())) 
print("BL#'s: ", BLS)

files = filedialog.askopenfilenames()
print("--------------")
print(files)
ExcelFileNames = [Path(x).name for x in files]
print("--------------")
print(type(ExcelFileNames))
print("--------------")
print(ExcelFileNames)
print("--------------")
print (ExcelFileNames[0])
print("--------------")
print("Number of files is:", len(ExcelFileNames))
print("--------------")
# read them in
excels = [pd.ExcelFile(name) for name in files]


# turn them into dataframes
frames = [x.parse(x.sheet_names[0], header=None,index_col=None, skiprows=4) for x in excels]
print(type(frames))
print("--------------")

# delete the first row for all frames except the first
# i.e. remove the header row -- assumes it's the first
frames[1:] = [df[1:] for df in frames[1:]]

for x in BLS
    print(x)
    frames[x].BOL = BLS
# this prints the first row of the first data frame -  print(frames[0].iloc[0])
# concatenate them..
combined = pd.concat(frames)


# write it out
combined.to_excel("DNcombined.xlsx", header=False, index=False)

1 个答案:

答案 0 :(得分:0)

您在此部分错过了一个冒号:

for x in BLS
    print(x)
    frames[x].BOL = BLS

它应该像这样:

for x in BLS:
    print(x)
    frames[x].BOL = BLS