简体   繁体   English

如何遍历 Excel 工作表和逆透视数据框。 然后将它们附加到一个 daraframe 中

[英]How to iterate through excel sheets and unpivot dataframes. Then append them together into one daraframe

I have Excel with 3 sheets: Gross, Margin, Revenue.我有 3 张 Excel 表格:毛额、利润率、收入。 Each of them has a table with same columns and row headers.他们每个人都有一个具有相同列和行标题的表格。

I need to :我需要 :

1) iterate through each sheet and save into a dataframe 1)遍历每个工作表并保存到数据框中

2) unpivot each dataframe 2) 取消每个数据框的旋转

3) append value columns from each daraframe into one. 3) 将每个 daraframe 中的值列附加为一个。

Gross:总的:

在此处输入图片说明

Margin:利润:

在此处输入图片说明

Revenue:收入:

在此处输入图片说明

Outcome should be like this:结果应该是这样的:

在此处输入图片说明

If I use sheet_name=None then I get an error:如果我使用sheet_name=None那么我会收到一个错误:

import pandas as pd
df = pd.read_excel('BudgetData.xlsx', sheet_name=None,index=False)
df_unpv = df.melt(id_vars=['Company'], var_name ='Month', value_name = 'Gross Revenue')
print(df_unpv)

Error I get:我得到的错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-60-ee1791c449b1> in <module>
      1 import pandas as pd
      2 df = pd.read_excel('BudgetData.xlsx', sheet_name=None,index=False)
----> 3 df_unpv = df.melt(id_vars=['Company'], var_name ='Month', value_name = 'Gross Revenue')
      4 df_unpv
      5 

AttributeError: 'collections.OrderedDict' object has no attribute 'melt'

Excel file with sample data can be found here: https://www.dropbox.com/s/9dsnylng70t5a8i/Count%20Open%20and%20Closed%20at%20Point%20of%20time.pbix?dl=0可以在此处找到包含示例数据的 Excel 文件: https : //www.dropbox.com/s/9dsnylng70t5a8i/Count%20Open%20and%20Closed%20at%20Point%20of%20time.pbix?dl=0

You are trying to call the .melt() method from your df object, instead of calling it from pd as the docs say:您正在尝试从df对象调用.melt()方法,而不是像文档所说的那样从pd调用它:

pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name='value', col_level=None)

Parameters:参数:
frame : DataFrame id_vars : tuple, list, or ndarray, optional Column(s) to use as identifier variables. frame : DataFrame id_vars : 元组、列表或 ndarray,可选 Column(s) 用作标识符变量。

value_vars : tuple, list, or ndarray, optional Column(s) to unpivot. value_vars : 元组、列表或 ndarray,可选的 Column(s) to unpivot。 If not specified, uses all columns that are not set as id_vars.如果未指定,则使用所有未设置为 id_vars 的列。

var_name : scalar Name to use for the 'variable' column. var_name :用于“变量”列的标量名称。 If None it uses frame.columns.name or 'variable'.如果 None 它使用 frame.columns.name 或“变量”。

value_name : scalar, default 'value' Name to use for the 'value' column. value_name :标量,默认 'value' 用于 'value' 列的名称。

col_level : int or string, optional If columns are a MultiIndex then use this level to melt. col_level : int 或 string,可选如果列是 MultiIndex 则使用此级别来融化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有效地遍历 Python 中选定的 Excel 工作表并将它们附加到数据框中? - How to efficiently iterate through selected Excel sheets in Python and append them into a Data Frame? Append 数据帧到多个 Excel 表 - Append dataframes to multiple Excel sheets 如何将 append 多个 Excel 文件中的所有工作表放入一个 Excel 文件(不将它们合并或组合成一张工作表) - How to append all sheets in multiple Excel files into One Excel file (not consolidating or combining them into one sheet) 有没有一种方法可以遍历多个数据框,以格式化将它们写入多个Excel工作表? - Is there a way to iterate over multiple dataframes to write them to multiple excel sheets with formatting? 遍历 excel 文件中的多个工作表,并在一行中的一个值和 append 所有工作表之后过滤所有数据 - Iterate through multiple sheets in excel file and filter all the data after a value in a row and append all the sheets Python:如何从一个 excel 文件中循环遍历多个工作表并将它们组合成一个 dataframe - Python: How to loop through multiple sheets from one excel file and combine them into one dataframe 如何在 pyspark 中将 append 数据帧一起? - How to append dataframes together in pyspark? 如何从 Excel 工作表创建嵌套字典并将它们合并在一起 - How to create nested dictionary from excel sheets and merge them together 如何同时遍历一个列表和一个 Daraframe 列表 - How to iterate over a list and a list of Daraframe at the same time 如何遍历两个数据框创建新的? - How to iterate through two dataframes create new one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM