替换多索引数据框中的特定值

时间:2020-07-02 13:44:53

标签: python pandas dataframe multi-index

我有一个具有3个索引级别和2个数字列的multindex数据框。

A   1   2017-04-01  14.0    87.346878
        2017-06-01  4.0     87.347504
    2   2014-08-01  1.0     123.110001
        2015-01-01  4.0     209.612503
B   3   2014-07-01  1.0     68.540001
        2014-12-01  1.0     64.370003
    4   2015-01-01  3.0     75.000000

我想在新的第二级索引开始的地方替换第三级索引的第一行中的值。 例如:每隔第一行

(A,1,2017-04-01)->0.0   0.0 
(A,2,2014-08-01)->0.0   0.0  
(B,3,2014-07-01)->0.0   0.0  
(B,4,2015-01-01)->0.0   0.0

数据帧太大,像df.xs('A,1')...df.xs(A,2)这样逐个数据帧地进行数据处理很费时。有什么方法可以让我得到一个面具,并用这些位置的新值替换?

4 个答案:

答案 0 :(得分:1)

level=2上使用DataFrame.reset_index,然后在level=[0, 1]上使用DataFrame.groupby,并使用level_2,然后使用pd.MultiIndex.from_arrays聚合first创建一个多级索引,最后使用此多级索引更改数据帧中的值:

idx = df.reset_index(level=2).groupby(level=[0, 1])['level_2'].first()
idx = pd.MultiIndex.from_arrays(idx.reset_index().to_numpy().T)
df.loc[idx, :] = 0

结果:

# print(df)
               col1        col2
A 1 2017-04-01  0.0    0.000000
    2017-06-01  4.0   87.347504
  2 2014-08-01  0.0    0.000000
    2015-01-01  4.0  209.612503
B 3 2014-07-01  0.0    0.000000
    2014-12-01  1.0   64.370003
  4 2015-01-01  0.0    0.000000

答案 1 :(得分:1)

我们可以使用以下方法提取一系列二级索引:

df.index.get_level_values(1)
# output: Int64Index([1, 1, 2, 2, 3, 3, 4], dtype='int64')

并检查其更改位置:

idx = df.index.get_level_values(1)
np.where(idx != np.roll(idx, 1))[0]
# output: array([0, 2, 4, 6])

因此,我们可以简单地将第二条语句的返回值与iloc一起使用,以获取每个第二级索引的第一行并按如下所示修改其值:

idx = df.index.get_level_values(1)
df.iloc[np.where(idx != np.roll(idx, 1))[0]] = 0

输出:

                  value1      value2
A 1 2017-04-01       0.0    0.000000
    2017-06-01       4.0   87.347504
  2 2014-08-01       0.0    0.000000
    2015-01-01       4.0  209.612503
B 3 2014-07-01       0.0    0.000000
    2014-12-01       1.0   64.370003
  4 2015-01-01       0.0    0.000000

答案 2 :(得分:1)

您可以在简单的<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/calendar" android:icon="@drawable/ic_calendar" android:title="@string/calendar" /> <item android:id="@+id/action_empty" android:checkable="false" android:checked="false" android:enabled="false" android:title="" app:showAsAction="always" /> <item android:id="@+id/charts" android:icon="@drawable/ic_charts" android:title="@string/cycles" /> </menu> 中使用石斑鱼indeces

iloc

示例:

df.iloc[[a[0] for a in df.groupby(level=[0, 1]).indices.values()]] = 0

结果:

df = pd.DataFrame({'col1': [14., 4., 1., 4., 1., 1., 3.],
                   'col2': [ 87.346878, 87.347504, 123.110001, 209.612503, 68.540001, 64.370003, 75.]},
                   index = pd.MultiIndex.from_tuples(([('A', 1, '2017-04-01'), ('A', 1, '2017-06-01'),
                                                       ('A', 2, '2014-08-01'), ('A', 2, '2015-01-01'),
                                                       ('B', 3, '2014-07-01'), ('B', 3, '2014-12-01'),
                                                       ('B', 4, '2015-01-01')])))

时间:

                col1        col2
A 1 2017-04-01   0.0    0.000000
    2017-06-01   4.0   87.347504
  2 2014-08-01   0.0    0.000000
    2015-01-01   4.0  209.612503
B 3 2014-07-01   0.0    0.000000
    2014-12-01   1.0   64.370003
  4 2015-01-01   0.0    0.000000

所以这比accepted answer快7倍

答案 3 :(得分:0)

我认为您可以使用类似这样的内容:

import pandas as pd
import numpy as np
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
   ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))

df = pd.DataFrame([['A', 'B'], ['bar', 'two'],
                   ['foo', 'one'], ['foo', 'two']],
                 columns=['first', 'second'])
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
df = pd.DataFrame(np.random.randn(8, 4), index=arrays)
df

您可以从索引中创建唯一值列表。然后获取索引位置,以在您的列上将行值与行值重合。

lst = ['bar','foo', 'qux']
ls = []
for i in lst:
    base = df.index.get_loc(i)
    a = base.indices(len(df))
    a = a[0]
    ls.append(a)
    
    for ii in ls:
    #print(ii)
        df[0][ii] = 0

df

幸运的是,这可以为您提供帮助。

干杯!