为什么追加不起作用?如何在一个新的 excel Sheet 中获取 excel 中的特定列
Why append dont work? how take specific columns from excel in one new excel Sheet
问题是:
我想从多个不同的 excell(.xlsx) filles、特定的 ccolumns 中获取并将所有这些保存在不同的 excell-Sheet..
我可以在我的终端中获取 DataFrame,但只将最后加载的 .xlsx 保存在我的 excell-sheet 中。
我做错了什么?如何解决这个问题?
pandas 有解决这个常见问题的简单命令吗?
我尝试了很多来自“Whosebug”的解决方案,但我找不到正确的方法..
import pandas as pd
import numpy as np
df_col=pd.DataFrame()
print(df_col)
i=0
while i<len(files):
# Import the excel file and call it xls_file
xls_file = pd.ExcelFile(files[i])
# Load the xls file's Sheet1 as a dataframe
df = xls_file.parse()
need_df = pd.read_excel(files[i], usecols=list_col_pros)
########################################################
# Create a Pandas Excel writer using XlsxWriter as the engine.
df_col.append(need_df)
##########################################################
# Returns column with label col as Series
print(need_df)
i=i+1
##########################
print(df_col)
writer = pd.ExcelWriter('all_pros.xlsx', engine='xlsxwriter')
# Write each dataframe to a different worksheet.
df_col.to_excel(writer, sheet_name='Sheet')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
所以这行得通。我制作了一些 B 列标题为“起始位置”的虚拟文件
但我认为您应该能够轻松地将其更改为您的 filenames/columns。
import pandas as pd
import numpy as np
df_col=pd.DataFrame()
print(df_col)
files = ["1.xlsx","2.xlsx","3.xlsx"]
i=0
while i<len(files):
# Import the excel file and call it xls_file
# xls_file = pd.ExcelFile(files[i])
# Load the xls file's Sheet1 as a dataframe
# df = xls_file.parse()
need_df = pd.read_excel(files[i], usecols="B")
########################################################
# Create a Pandas Excel writer using XlsxWriter as the engine.
df_col[files[i]] = need_df['First Position'].values
##########################################################
# Returns column with label col as Series
print(need_df)
i=i+1
##########################
print(df_col)
writer = pd.ExcelWriter('all_pros.xlsx', engine='xlsxwriter')
# Write each dataframe to a different worksheet.
df_col.to_excel(writer, sheet_name='Sheet')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
问题是: 我想从多个不同的 excell(.xlsx) filles、特定的 ccolumns 中获取并将所有这些保存在不同的 excell-Sheet.. 我可以在我的终端中获取 DataFrame,但只将最后加载的 .xlsx 保存在我的 excell-sheet 中。 我做错了什么?如何解决这个问题? pandas 有解决这个常见问题的简单命令吗? 我尝试了很多来自“Whosebug”的解决方案,但我找不到正确的方法..
import pandas as pd
import numpy as np
df_col=pd.DataFrame()
print(df_col)
i=0
while i<len(files):
# Import the excel file and call it xls_file
xls_file = pd.ExcelFile(files[i])
# Load the xls file's Sheet1 as a dataframe
df = xls_file.parse()
need_df = pd.read_excel(files[i], usecols=list_col_pros)
########################################################
# Create a Pandas Excel writer using XlsxWriter as the engine.
df_col.append(need_df)
##########################################################
# Returns column with label col as Series
print(need_df)
i=i+1
##########################
print(df_col)
writer = pd.ExcelWriter('all_pros.xlsx', engine='xlsxwriter')
# Write each dataframe to a different worksheet.
df_col.to_excel(writer, sheet_name='Sheet')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
所以这行得通。我制作了一些 B 列标题为“起始位置”的虚拟文件 但我认为您应该能够轻松地将其更改为您的 filenames/columns。
import pandas as pd
import numpy as np
df_col=pd.DataFrame()
print(df_col)
files = ["1.xlsx","2.xlsx","3.xlsx"]
i=0
while i<len(files):
# Import the excel file and call it xls_file
# xls_file = pd.ExcelFile(files[i])
# Load the xls file's Sheet1 as a dataframe
# df = xls_file.parse()
need_df = pd.read_excel(files[i], usecols="B")
########################################################
# Create a Pandas Excel writer using XlsxWriter as the engine.
df_col[files[i]] = need_df['First Position'].values
##########################################################
# Returns column with label col as Series
print(need_df)
i=i+1
##########################
print(df_col)
writer = pd.ExcelWriter('all_pros.xlsx', engine='xlsxwriter')
# Write each dataframe to a different worksheet.
df_col.to_excel(writer, sheet_name='Sheet')
# Close the Pandas Excel writer and output the Excel file.
writer.save()