Pandas: 条件选择列根据另一列的Header进行计算
Pandas: Conditionally Selecting Columns to perform Calculation based on Header of another Column
我的数据框如下所示:
(1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8) (1, 9) (1, 10) (1, 11) ... 2 3 4 5 6 7 8 9 10 11
0 0 1 0 1 1 1 1 0 1 0 ... 0.612544 0.727393 0.366578 0.631451 0.722980 0.772853 0.964982 0.549801 0.406692 0.798083
1 0 0 0 0 0 0 0 0 0 0 ... 0.583228 0.698729 0.343934 0.602037 0.694230 0.745422 0.954682 0.521298 0.382381 0.771640
2 1 0 0 1 0 1 1 0 0 0 ... 0.481291 0.593353 0.271028 0.498949 0.588807 0.641602 0.901779 0.424495 0.303309 0.669657
3 1 1 0 1 0 1 1 0 0 1 ... 0.583228 0.698729 0.343934 0.602037 0.694230 0.745422 0.954682 0.521298 0.382381 0.771640
4 0 0 0 1 1 1 1 1 1 1 ... 0.612544 0.727393 0.366578 0.631451 0.722980 0.772853 0.964982 0.549801 0.406692 0.798083
我的列 headers 带有元组,例如 (1, 2)
,列 headers 是单个元素,例如 1
。我想根据具有该元组元素的列对元组列执行计算。例如,对于元组 (1, 2)
,我想检索列 1
和 2
,将它们相乘,然后从列 (1, 2)
中减去结果。
我想到的解决方案是创建 (55) 个新列,从仅包含单个元素(例如 1
或 2
)的列执行第一次计算,然后使用 .where()
和 all()
语句进行某种身份匹配。但是,这在计算上似乎相当低效,因为我要制作另一组数据,而不是直接在元组列上执行计算。我该怎么做?
不确定这是否更快,但这是一个不需要 where()/all() 的解决方案
import pandas as pd
# create some sample data
arr = [[1, 2, 3, 4, 5, 6, 7],
[7, 6, 5, 4, 3, 2, 1]]
df = pd.DataFrame(arr, columns=[('a', 'b'), ('c','d'), ('a', 'd'), 'a', 'b', 'c', 'd'])
# get all tuple headers
tuple_columns = [col for col in df.columns if isinstance(col, tuple)]
# put the results into a list of series and concat into a DataFrame
results = pd.concat([df[col] - df[col[0]] * df[col[1]] for col in tuple_columns], axis=1)
# rename the columns
results.columns = tuple_columns
我的数据框如下所示:
(1, 2) (1, 3) (1, 4) (1, 5) (1, 6) (1, 7) (1, 8) (1, 9) (1, 10) (1, 11) ... 2 3 4 5 6 7 8 9 10 11
0 0 1 0 1 1 1 1 0 1 0 ... 0.612544 0.727393 0.366578 0.631451 0.722980 0.772853 0.964982 0.549801 0.406692 0.798083
1 0 0 0 0 0 0 0 0 0 0 ... 0.583228 0.698729 0.343934 0.602037 0.694230 0.745422 0.954682 0.521298 0.382381 0.771640
2 1 0 0 1 0 1 1 0 0 0 ... 0.481291 0.593353 0.271028 0.498949 0.588807 0.641602 0.901779 0.424495 0.303309 0.669657
3 1 1 0 1 0 1 1 0 0 1 ... 0.583228 0.698729 0.343934 0.602037 0.694230 0.745422 0.954682 0.521298 0.382381 0.771640
4 0 0 0 1 1 1 1 1 1 1 ... 0.612544 0.727393 0.366578 0.631451 0.722980 0.772853 0.964982 0.549801 0.406692 0.798083
我的列 headers 带有元组,例如 (1, 2)
,列 headers 是单个元素,例如 1
。我想根据具有该元组元素的列对元组列执行计算。例如,对于元组 (1, 2)
,我想检索列 1
和 2
,将它们相乘,然后从列 (1, 2)
中减去结果。
我想到的解决方案是创建 (55) 个新列,从仅包含单个元素(例如 1
或 2
)的列执行第一次计算,然后使用 .where()
和 all()
语句进行某种身份匹配。但是,这在计算上似乎相当低效,因为我要制作另一组数据,而不是直接在元组列上执行计算。我该怎么做?
不确定这是否更快,但这是一个不需要 where()/all() 的解决方案
import pandas as pd
# create some sample data
arr = [[1, 2, 3, 4, 5, 6, 7],
[7, 6, 5, 4, 3, 2, 1]]
df = pd.DataFrame(arr, columns=[('a', 'b'), ('c','d'), ('a', 'd'), 'a', 'b', 'c', 'd'])
# get all tuple headers
tuple_columns = [col for col in df.columns if isinstance(col, tuple)]
# put the results into a list of series and concat into a DataFrame
results = pd.concat([df[col] - df[col[0]] * df[col[1]] for col in tuple_columns], axis=1)
# rename the columns
results.columns = tuple_columns