python - 多索引数据帧内的操作

标签 python pandas

我正在使用多索引数据帧,并且想要执行一些我正在努力解决的操作:

a) 我想在不使用 for 循环的情况下对列表应用多个操作(按元素)

b) 我想提取 DataFrame 的索引值并比较这些值;在它们必须从对象转换为 int 或 float 之前

c) 我想比较 DataFrame 中的值(不使用 for 循环),并根据比较的值从任一列中选择值

================================================== =========================

import pandas as pd
import numpy as np

idx = pd.IndexSlice
ix = pd.MultiIndex.from_product(
    [['2015', '2016', '2017', '2018'],
     ['2016', '2017', '2018', '2019', '2020'],
     ['A', 'B', 'C']],
    names=['SimulationStart', 'ProjectionPeriod', 'Group']
)

df = pd.DataFrame(np.random.randn(60, 1), index=ix, columns=['Origin'])
origin = df.loc[idx[:, :, :], 'Origin'].values

increase_over_base_percent = 0.3
increase_over_base_abs = 10
abs_level = 1
min_increase = 0.001

'Is there a way to do this comparison without using for loops?'
# The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
change = pd.Series(np.nan)
i = 0
for element in origin:
    change[i] = max(
        min(element * (1 + increase_over_base_percent),
            element + increase_over_base_abs,
            abs_level),
        element + min_increase)
    i += 1

print(change)


# Write results to a new column in the DataFrame ('Change')
df.loc[idx[:, :, :], 'Change'] = change

# Add data on 'Group' level
group_qualifier = [0, 0, 1]

# Is there a way to apply the group_qualifier to the group level without having to slice each index?
# Note: the formula does not work yet (results are to be reported in a new column of the DataFrame)
df.loc[idx[:], 'GroupQA'] = group_qualifier

'This is the part I am struggling with most (my index values are objects, not integers or floats;'
'and the comparison of values within the DataFrame does not work either)'
# Create new column 'Selected'; use origin values for all combinations where
# projectionPeriod < simulationStart & group_qualifier value == 0;
# use change values for all other combinations
values = df.index.get_level_values
mask = (values('ProjectionPeriod') - values('SimulationStart')) <= 1
mask = mask * df.loc[idx[:], 'GroupQA'].values
selected = df.loc[mask]
df.loc[idx[:, :, :], 'Selected'] = selected

最佳答案

a)的部分答案:

df['Change'] = pd.concat([
    pd.concat([
        df.loc[:, 'Origin'] * (1 + increase_over_base_percent),
        df.loc[:, 'Origin'] + increase_over_base_abs,
    ], axis=1).min(axis=1).clip(upper=abs_level),
    df.loc[:, 'Origin'] + min_increase
], axis=1).max(axis=1)

这个想法是直接在 Origin 系列上使用 pandas 的 minmax 函数(稍作修改,使用 abs_level 的剪辑)。

由于 pandas 操作保留索引,因此您可以直接将结果分配给列。


编辑:如果您愿意,可以使用this question末尾解释的组合方法。 .

关于python - 多索引数据帧内的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40419550/

相关文章:

python - 蜘蛛scrapy中的读取设置

python - 在迭代期间更改 python 序列大小

python - 加快将大 excel 文件导入 pandas 数据框的速度

python - 使用循环创建带有 Dataframe Pandas 的 Excel 工作表

python - 从现有的日期列创建新列 "Week"

python - 当尝试读取文件名来创建自定义数据集时,改为读取矢量信息

python - 通过 python 将值传递到 jinja2 中定义的范围

python - 在 R 单元、rpy2、Jupyter Notebook 中使用 pandas 数据帧时出错

python - 将嵌套的 MongoDB 导入 Pandas

python - 如何计算数据框中两列之间的相关系数?