我正在使用python,熊貓和numpy來讀取一些數據。熊貓:如何將矩陣與不同的索引和列結合起來?
我有兩個數據幀:
輸入1-成本矩陣(它具有每季節和地區的費用):索引=區域和列=季節 輸入2-二進制矩陣(值1時一個月「 a「屬於季節」b「:index = seasons,columns = months
我想要的輸出是一個矩陣C,它具有每個區域和月份的成本:index = region,column month。
任何人都可以請幫我嗎?我用Google搜索了很多,但我不能找到解決方案。
我的代碼更新
import pandas as pd
import numpy as np
from xlwings import Workbook, Range
import os
print(os.getcwd())
link = (os.getcwd() + '/test.xlsx')
print(link)
#Open the Workbook
wb = Workbook(link)
#
#Reading data
regions=np.array(Range('Sheet1','regions').value)
#[u'Region A' u'Region B' u'Region C' u'Region D']
seasons=np.array(Range('Sheet1','seasons').value)
#[u'Season A' u'Season B' u'Season C' u'Season D']
months=np.array(Range('Sheet1','months').value)
#[u'Jan' u'Feb' u'Mar' u'Apr' u'May' u'Jun' u'Jul' u'Aug']
#read relationship between season and month
data=Range('Sheet1','rel').table.value
relationship=pd.DataFrame(data[0:], index = regions, columns=months)
# Jan Feb Mar Apr May Jun Jul Aug
#Region A 1 1 0 0 0 0 0 0
#Region B 0 0 1 1 0 0 0 0
#Region C 0 0 0 0 1 1 0 0
#Region D 0 0 0 0 0 0 1 1
#read the cost per region
data=Range('Sheet1','cost').table.value
cost=pd.DataFrame(data[0:], index = regions, columns=seasons)
# Season A Season B Season C Season D
#Region A 1 9 7 2
#Region B 7 0 3 3
#Region C 4 0 7 5
#Region D 3 10 3 10
#What I want:
# Jan Feb Mar Apr May Jun Jul Aug
#Region A 1 1 9 9 7 7 2 2
#Region B 7 7 0 0 3 3 3 3
#Region C 4 4 0 0 7 7 5 5
#Region D 3 3 10 10 3 3 10 10
你能提供你的數據框的樣本數據嗎? –