简体   繁体   English

向多索引 dataframe 上的每个索引添加一行

[英]Adding a row to each index on a multi-indexed dataframe

I have a multi-indexed dataframe, and I want to add to every one of the most outer index another line, where the two other indices are marked with a specific string (Same string for all indices in all values).我有一个多索引 dataframe,我想在最外层索引的每一个中添加另一行,其中其他两个索引用特定字符串标记(所有值中的所有索引都使用相同的字符串)。 The other values of that row can be empty or anything else.该行的其他值可以为空或其他任何值。

I tried creating a different dataframe using groupby and appending them but I can't get the indices to work.我尝试使用 groupby 创建一个不同的 dataframe 并附加它们,但我无法让索引工作。

For example, for the dataframe:例如,对于 dataframe:

Index1  Index2  Index3  val
A        d       1       a
A        d       2       b
A        e       3       c
A        e       4       d
B        f       5       e
B        f       6       f
B        g       7       g
C        h       8       h
C        h       9       i 
C        i       10      j

I would like to get:我想得到:

Index1  Index2  Index3  val
A        d       1       a
A        d       2       b
A        e       3       c
A        e       4       d
A        StringA StringA <any value>
B        f       5       e
B        f       6       f
B        g       7       g
B        StringA StringA <any value>
C        h       8       h
C        h       9       i 
C        i       10      j
C        StringA StringA <any value>

IIUC IIUC

s=pd.DataFrame({'Index1':df.Index1.unique(),
              'Index2':df.Index1.radd('String').unique(),
              'Index3': df.Index1.radd('String').unique(),
              'val':[1]*df.Index1.nunique()})
pd.concat([df.reset_index(),s]).sort_values('Index1').set_index(['Index1','Index2','Index3'])
Out[301]: 
  Index1   Index2   Index3 val
0      A        d        1   a
1      A        d        2   b
2      A        e        3   c
3      A        e        4   d
0      A  StringA  StringA   1
4      B        f        5   e
5      B        f        6   f
6      B        g        7   g
1      B  StringB  StringB   1
7      C        h        8   h
8      C        h        9   i
9      C        i       10   j
2      C  StringC  StringC   1

You can unstack, assign, stack:您可以取消堆叠、分配、堆叠:

new_df = df.unstack(level=(-1,-2))

# you can pass a series here
new_df[('val','StringA','StringA')] = 'ABC'

new_df.stack(level=(-1,-2))

Output: Output:

                        val
Index1 Index2  Index3      
A      d       1          a
               2          b
       e       3          c
               4          d
       StringA StringA  ABC
B      f       5          e
               6          f
       g       7          g
       StringA StringA  ABC
C      h       8          h
               9          i
       i       10         j
       StringA StringA  ABC

Or try using:或尝试使用:

groupby = df.groupby(df['Index1'], as_index=False).last()
groupby[['Index2', 'Index3', 'val']] = ['StringA', 'StringA', np.nan]
df = pd.concat([df, groupby]).sort_values(['Index1', 'Index3']).reset_index()
print(df)

Output: Output:

    index Index1   Index2   Index3  val
0       0      A        d        1    a
1       1      A        d        2    b
2       2      A        e        3    c
3       3      A        e        4    d
4       0      A  StringA  StringA  NaN
5       4      B        f        5    e
6       5      B        f        6    f
7       6      B        g        7    g
8       1      B  StringA  StringA  NaN
9       7      C        h        8    h
10      8      C        h        9    i
11      9      C        i       10    j
12      2      C  StringA  StringA  NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM