首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将多个列、多个文件的数据合并成一个数据帧?

如何将多个列、多个文件的数据合并成一个数据帧?
EN

Stack Overflow用户
提问于 2019-08-16 15:11:50
回答 2查看 82关注 0票数 0

我有以下数据框:

代码语言:javascript
复制
  sp_id         sp_dt          v1      v1      v3

x1|x2|x30|x40   2018-10-07     100     200     300 
x1|x2|x30|x40   2018-10-14     80       80      90  
x1|x2|x30|x40   2018-10-21     34       35      36 
x1|x2|x31|x41   2018-10-07     100     200     300 
x1|x2|x31|x41   2018-10-14     80       80      90  
x1|x2|x31|x41   2018-10-21     34       35      36   
....
x1|x2|x39|x49   2018-10-21     340      350     36

以及包含以下数据的excel文件( excel中的每个工作表可能包含多个变量,如v4、v5,如下所示,也可能包含另一个工作表中的v6 ):

代码语言:javascript
复制
Variable      sp_partid1  sp_partid2    2018-10-07  ... 2018-10-21
  v4            x30         x40              160     ...   154
  v4            x31         x41              59      ...   75
  ....
  v4            x39         x49              75      ...   44
  v5            x30         x40              16      ...   24
  v5            x31         x41              59      ...   79
  ....
  v5            x39         x49              75      ...   34

sp_partid1和sp_partid2是可选的列。它们是顶部数据框中的"part of sp_id“列。该文件可以没有列,或者在该特定示例中最多具有4个这样的列,每个列是顶部数据框中的sp_id列的一部分。

最终输出应如下所示:

代码语言:javascript
复制
  sp_id         sp_dt          v1      v1      v3     v4    v5
x1|x2|x30|x40   2018-10-07     100     200     300    160   16  
x1|x2|x30|x40   2018-10-14     80       80      90    ...   ...
x1|x2|x30|x40   2018-10-21     34       35      36    154   24
x1|x2|x31|x41   2018-10-07     100     200     300    59    59
x1|x2|x31|x41   2018-10-14     80       80      90    ...   ...
x1|x2|x31|x41   2018-10-21     34       35      36    75    79
....
x1|x2|x39|x49   2018-10-21     340      350     36    44    34

Edit1启动:输出是如何生成的?

代码语言:javascript
复制
get a list of variables
check if the variable(say v4 in this case) exists in any sheet
if it does:
  does it have any "part of sp_id" 
  #In the example shown sp_partid1 and sp_partid2 of excel sheets 
  #are part of sp_id of dataframe.
  if yes:
  #it means the part of sp_id is common for all values. (x1|x2) in this case. 
      add a new column to dataframe, v4, which has sp_id, sp_dt and,
      the value of that date 
  if no:
  #it means the whol sp_id is common for all values. (x1|x2|x3|x4) in this case and not shown in example.
      add a new column to dataframe, v4, and copy the value under the appropriate dates in excel sheet into corresponding v4 values and sp_dt

例如,160是v4、x30、x40的2018-10-07下的值,因此最终输出中的v4在第一行显示160。

Edit1结束:

我的代码是这样开始的:

代码语言:javascript
复制
df # is the top data frame which I have not gotten around to using yet
var_value # gets values in a loop like 'v4, v5...'

sheets_dict = {name: pd.read_excel('excel_file.xlsx', sheet_name = name, parse_dates = True) for name in sheets}

for key, value in sheets_dict.items():
   if 'Variable' in value.columns:
   # 'Variable' column exists in this sheet
      if var_value in value['Variable'].values:
      # var_value exists in 'Variable' column (say, v4)
          for column in value.columns:
             if column.startswith('sp_'):
                #Do something with column values, then map the values etc
EN

回答 2

Stack Overflow用户

发布于 2019-08-16 16:57:23

假设您的一个excel表格包含以下数据,

代码语言:javascript
复制
  Variable sp_partid1 sp_partid2  2018-10-07  2018-10-08  2018-10-21
0       v4        x30        x40         160        10.0         154
1       v4        x31        x41          59         NaN          75
2       v4        x32        x42          75        10.0          44
3       v5        x30        x40          16        10.0          24
4       v5        x31        x41          59        10.0          79
5       v5        x32        x42          75        10.0          34

您可以组合使用pandas meltpivot_table函数来获得想要的结果。

代码语言:javascript
复制
import pandas as pd
book= pd.read_excel('del.xlsx',sheet_name=None)
for df in book.values():
    df=df.melt(id_vars=['Variable','sp_partid1','sp_partid2'], var_name="Date", value_name="Value")
    # concatenate strings of two columns separated by a '|'
    df['sp_id'] = df['sp_partid1'] +'|'+ df['sp_partid2']
    df = df.loc[:,['Variable', 'sp_id','Date','Value']]
    df = df.pivot_table('Value', ['sp_id','Date'], 'Variable').reset_index( drop=False )
    print(df)  

>> output
Variable    sp_id        Date     v4    v5
0         x30|x40  2018-10-07  160.0  16.0
1         x30|x40  2018-10-08   10.0  10.0
2         x30|x40  2018-10-21  154.0  24.0
3         x31|x41  2018-10-07   59.0  59.0
4         x31|x41  2018-10-08    NaN  10.0
5         x31|x41  2018-10-21   75.0  79.0
6         x32|x42  2018-10-07   75.0  75.0
7         x32|x42  2018-10-08   10.0  10.0
8         x32|x42  2018-10-21   44.0  34.0

使用sheet_name=None读取excel工作簿将得到一个字典,其中worksheet namekeydata framevalue

票数 0
EN

Stack Overflow用户

发布于 2019-08-16 17:10:44

您正在尝试做的事情是有意义的,但它是一个相当长的操作序列,所以您在实现它时遇到一些问题是很正常的。我认为您应该回到关系数据库的更高抽象级别,并使用pandas提供的高级数据帧操作。

让我们从高级操作的角度总结一下您想要做的事情:

  1. 更改了sheet_dicts数据帧的格式,使其具有相同的数据,但以不同的方式呈现

代码语言:javascript
复制
   id3           id4        date            v4         v5       
   x30           x40        2018-10-07      160        154
   x31           x41        2018-10-08      30         10

  1. 将原始数据帧的id拆分为多个数据帧,生成的数据帧具有id和date.

上的原始数据帧

我不能给你一个精确的实现,因为你的规范仍然相当模糊,即使全局目标是明确的。此外,我没有提供参考资料来指导您使用关系数据库,但我强烈建议您了解情况,这将为您节省大量时间,特别是如果您经常需要执行此类任务。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57520330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档