【问题标题】:how to assign a value to the first row of every level zero group in pandas multiindex如何为熊猫多索引中每个零级组的第一行赋值
【发布时间】:2020-12-31 15:19:34
【问题描述】:

我正在尝试将值分配给多索引数据帧中每个零级组的第一行。第一行是日期和时间,不是行间的通用值。我附上了一个代码示例来重现一个最小的数据框。

import pandas as pd
import numpy as np

np.random.seed(123)

arrays = [np.array(["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"]),np.array(["one", "two", "not_one", "two", "not_one_one", "two", "not_not_one", "two"]),]
df = pd.DataFrame(np.random.randn(8,4), index=arrays)

我尝试过使用和不使用列索引来分配值。

df.loc[[df.groupby(level=0).nth(0)],'0'] = 100

xy = list(set(df.index.get_level_values(level=0)))
for ind1 in xy:
    df.loc[(ind1, df.iloc[0]),'0'] = 100

我已经尝试了大约 100 种不同的 iloc 变体,但都没有成功,我已经尝试了 xs

df.groupby(level=0).xs(0, level=1) = 100

我试过了

df.loc[df.groupby(level=0).nth(0)] = 100

还有,

df.groupby(level=0).nth(0) = 100

我已经为此花费了几个小时,但仍然没有得到任何结果。任何帮助,将不胜感激。 我想从这里开始:

                        0         1         2         3
bar one         -1.085631  0.997345  0.282978 -1.506295
    two         -0.578600  1.651437 -2.426679 -0.428913
baz not_one      1.265936 -0.866740 -0.678886 -0.094709
    two          1.491390 -0.638902 -0.443982 -0.434351
foo not_one_one  2.205930  2.186786  1.004054  0.386186
    two          0.737369  1.490732 -0.935834  1.175829
qux not_not_one -1.253881 -0.637752  0.907105 -1.428681
    two         -0.140069 -0.861755 -0.255619 -2.798589

到这里:

                        0         1         2         3
bar one               100  0.997345  0.282978 -1.506295
    two         -0.578600  1.651437 -2.426679 -0.428913
baz not_one           100 -0.866740 -0.678886 -0.094709
    two          1.491390 -0.638902 -0.443982 -0.434351
foo not_one_one       100  2.186786  1.004054  0.386186
    two          0.737369  1.490732 -0.935834  1.175829
qux not_not_one       100 -0.637752  0.907105 -1.428681
    two         -0.140069 -0.861755 -0.255619 -2.798589

【问题讨论】:

    标签: python pandas dataframe multi-index


    【解决方案1】:

    您可以使用groupby + cumcount 为每个level=0 组创建顺序计数器,然后使用带有loc 的布尔索引来更新列0 中的值,其中计数器为0

    df.loc[df.groupby(level=0).cumcount().eq(0), 0] = 100
    

                              0         1         2         3
    bar one          100.000000  0.997345  0.282978 -1.506295
        two           -0.578600  1.651437 -2.426679 -0.428913
    baz not_one      100.000000 -0.866740 -0.678886 -0.094709
        two            1.491390 -0.638902 -0.443982 -0.434351
    foo not_one_one  100.000000  2.186786  1.004054  0.386186
        two            0.737369  1.490732 -0.935834  1.175829
    qux not_not_one  100.000000 -0.637752  0.907105 -1.428681
        two           -0.140069 -0.861755 -0.255619 -2.798589
    

    【讨论】:

      猜你喜欢
      • 2021-08-28
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 2023-02-14
      • 2023-04-02
      • 2018-09-01
      相关资源
      最近更新 更多