我有一个看起来像这样的数据框
data = {'0': {('Field A', '0'): 10,
('Field A', '1'): 10,
('Field A', '2'): 10,
('Field B','0'): 6,
('Field B','1'): 6,
('Field B', '2'): 6},
'48': {('Field A', '0'): 5,
('Field A', '1'): 2,
('Field A', '2'): 1,
('Field B', '0'): 3,
('Field B', '1'): 2,
('Field B', '2'): 1}}
df = pd.DataFrame(data)
df.index.names = ['field', 'well']
df.T.index.names = ['interval']
print(df)
interval 0 48
field well
field A 0 10 5
1 10 2
2 10 1
field B 0 6 3
1 6 2
2 6 1
我正在试图弄清楚如何将所有列按元素划分为“Interval 0”。
结果应为:
interval 0 48
field well
Total Number of End Points 0 1.0 0.500000
1 1.0 0.200000
2 1.0 0.100000
Total Vessel Length 0 1.0 0.500000
1 1.0 0.333333
2 1.0 0.166667
答案 0 :(得分:1)
您可以使用div()
方法(与divide()
相同,参数axis
设置为0,以便按行元素划分执行:
df.div(df['0'], axis = 0)
#interval 0 48
#field well
#Field A 0 1.0 0.500000
# 1 1.0 0.200000
# 2 1.0 0.100000
#Field B 0 1.0 0.500000
# 1 1.0 0.333333
# 2 1.0 0.166667