简体   繁体   English

熊猫:从另一个数据框中的列值插入数据框中的行

[英]pandas: insert rows in a dataframe from column values in another dataframe

I've two multi-indexed dataframes with data as shown below:我有两个带有数据的多索引数据框,如下所示:

df1: df1:

                                                             qty
plot name sub plot name        crop          date                   
BL1       Cabbage-041221       Cabbage       2022-03-01    36.000000
                                             2022-03-02    29.900000
                                             2022-03-03    51.799999
                                             2022-03-16    48.000000
          Cluster Beans-110222 Cluster Beans 2022-04-15     5.500000
                                             2022-04-19     2.500000
          Musk Melon-050222    Musk Melon    2022-04-05    21.100000
          Tomato-050222        Tomato        2022-04-14    39.000000
BL2       Green Tomato-180122  Green Tomato  2022-03-15     7.500000
                                             2022-03-16     5.100000
                                             2022-03-17     3.700000
          Ladies Finger-191121 Ladies Finger 2022-03-02    12.600000
                                             2022-03-03     9.200000
                                             2022-03-04     7.200000
                                             2022-03-05     7.600000
Totals                                                  35199.440001

and df2:和df2:

                                                qty
plot name sub plot name        crop                      
BL1       Cabbage-041221       Cabbage         606.900003
          Cluster Beans-110222 Cluster Beans     8.000000
          Musk Melon-050222    Musk Melon       21.100000
          Tomato-050222        Tomato           39.000000
BL2       Green Tomato-180122  Green Tomato     27.200000
          Ladies Finger-191121 Ladies Finger    93.100000
          Ladies Finger-290122 Ladies Finger   139.650002

What I need is to combine the two dataframes so I can add a row to df1 for each crop category with the value in qty in df2 as follows:我需要将两个数据框组合起来,这样我就可以为每个作物类别在 df1 中添加一行,其中 df2 中的 qty 值如下:

                                                           qty
plot name sub plot name        crop          date                   
BL1       Cabbage-041221       Cabbage       2022-03-01    36.000000
                                             2022-03-02    29.900000
                                             2022-03-03    51.799999
                                             2022-03-16    48.000000
                                             Subtotal      606.900003
          Cluster Beans-110222 Cluster Beans 2022-04-15     5.500000
                                             2022-04-19     2.500000
                                             Subtotal       8.000000
          Musk Melon-050222    Musk Melon    2022-04-05    21.100000
                                             Subtotal      21.100000
          Tomato-050222        Tomato        2022-04-14    39.000000
                                             Subtotal      39.000000
BL2       Green Tomato-180122  Green Tomato  2022-03-15     7.500000
                                             2022-03-16     5.100000
                                             2022-03-17     3.700000
                                             Subtotal      27.200000
          Ladies Finger-191121 Ladies Finger 2022-03-02    12.600000
                                             2022-03-03     9.200000
                                             2022-03-04     7.200000
                                             2022-03-05     7.600000
                                             Subtotal      93.100000
Totals                                                  35199.440001

How do I go about doing this without losing the multi-index?如何在不丢失多索引的情况下执行此操作? df2 was obtained by applying a groupby on df1 as this: df2 是通过在 df1 上应用 groupby 获得的,如下所示:

df2 = (df1.drop('Totals', level=0)
.groupby(level=list(range(df1.index.nlevels-1))).sum())

Create a new column date in df2 with the value Subtotal then concatenate both dataframes and sort index:df2中使用值Subtotal创建一个新列date ,然后连接两个数据框和排序索引:

out = (pd.concat([df1, df2.assign(date='Subtotal').set_index('date', append=True)])
         .sort_index())

Output:输出:

>>> out
                                                                  qty
plot name sub plot name        crop          date                    
BL1       Cabbage-041221       Cabbage       2022-03-01     36.000000
                                             2022-03-02     29.900000
                                             2022-03-03     51.799999
                                             2022-03-16     48.000000
                                             Subtotal      606.900003
          Cluster Beans-110222 Cluster Beans 2022-04-15      5.500000
                                             2022-04-19      2.500000
                                             Subtotal        8.000000
          Musk Melon-050222    Musk Melon    2022-04-05     21.100000
                                             Subtotal       21.100000
          Tomato-050222        Tomato        2022-04-14     39.000000
                                             Subtotal       39.000000
BL2       Green Tomato-180122  Green Tomato  2022-03-15      7.500000
                                             2022-03-16      5.100000
                                             2022-03-17      3.700000
                                             Subtotal       27.200000
          Ladies Finger-191121 Ladies Finger 2022-03-02     12.600000
                                             2022-03-03      9.200000
                                             2022-03-04      7.200000
                                             2022-03-05      7.600000
                                             Subtotal       93.100000
          Ladies Finger-290122 Ladies Finger Subtotal      139.650002
Totals    NaN                  NaN           NaN         35199.440001

暂无
暂无

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

相关问题 在 Pandas 数据框中查找值并将数据插入到另一个 Pandas 数据框的列中 - Find values in a Pandas dataframe and insert the data in a column of another Pandas dataframe Pandas DataFrames:如何根据另一个数据帧列中的值使用现有数据帧中的索引值定位行? - Pandas DataFrames: How to locate rows using index values in existing dataframe based on values from another dataframe column? 使用另一个DataFrame中的行的值查找并替换pandas.Dataframe中的列标题 - Lookup and replace column headings in a pandas.Dataframe with the values from rows in another DataFrame Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value 来自 Pandas DataFrame 的 Select 行与另一个 DataFrame 中的列值完全相同 - Select rows from a Pandas DataFrame with exactly the same column values in another DataFrame pandas 在列值匹配时使用来自另一个数据帧的值更新数据帧 - pandas update a dataframe with values from another dataframe on the match of column values 熊猫:从另一列修改数据框中的值 - pandas: modifying values in dataframe from another column 熊猫数据框根据其他列值的范围插入值 - pandas dataframe insert values according to range of another column values Pandas Dataframe - 按列值过滤 dataframe 行 - Pandas Dataframe - Filtering dataframe rows by column values 给定 pandas DataFrame 中一列中的值列表,如何从同一行中的另一列中获取 output 中的值? - Given a list of values in a column in pandas DataFrame, how to output values from another column in the same rows?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM