按照分组显示来自其他列的相同行值

huangapple go评论63阅读模式
英文:

groupby to show same row value from other columns

问题

使用以下代码段通过“Mode”列分组并从“max, min”的“indicator”中提取值后,让相对值显示在与下方相同的数据框中:

df = pd.read_csv(r'relative.csv')
Grouped = df.groupby('Mode')['Indicator'].agg(['max', 'min'])
print(Grouped)


<details>
<summary>英文:</summary>

[![enter image description here][1]][1]

After groupby by &quot;Mode&quot; column and take out the value from &quot;indicator&quot; of &quot;max, min&quot;, how to let the relative value to show in the same dataframe like below:

df = pd.read_csv(r'relative.csv')
Grouped = df.groupby('Mode')['Indicator'].agg(['max', 'min'])
print(Grouped)

[![enter image description here][2]][2]

(from google, maybe can use from col_value or row_value function, but seem be more complicated, could someone can help to solve it by easy ways? thank you.)


  [1]: https://i.stack.imgur.com/bl63Q.png
  [2]: https://i.stack.imgur.com/2Ynod.png

</details>


# 答案1
**得分**: 1

您可以在两个步骤中完成,使用`groupby`和`idxmin()`或`idxmax()`:

```python
# 创建一个包含'Indicator'的最小值的DataFrame,将列名'Value'重命名为'B'
min = df.loc[df.groupby('Mode')['Indicator'].idxmin()].reset_index(drop=True).rename(columns={'Indicator': 'min', 'Value': 'B'})
print(min)
#   Mode  min  B
# 0    A    1  6
# 1    B    1  7

# 创建一个包含'Indicator'的最大值的DataFrame,将列名'Value'重命名为'A'
max = df.loc[df.groupby('Mode')['Indicator'].idxmax()].reset_index(drop=True).rename columns={'Indicator': 'max', 'Value': 'A'})
print(max)
#   Mode  max  A
# 0    A    3  2
# 1    B    4  3

# 合并这两个DataFrame
result = pd.merge(min, max)

# 重新排列列以匹配期望的输出
print(result[['Mode', 'max', 'min', 'A', 'B']])
#   Mode  max  min  A  B
# 0    A    3    1  2  6
# 1    B    4    1  3  7
英文:

You can do it in two steps, using groupby and idxmin() or idxmix():

# Create a df with the min values of &#39;Indicator&#39;, renaming the column &#39;Value&#39; to &#39;B&#39;
min = df.loc[df.groupby(&#39;Mode&#39;)[&#39;Indicator&#39;].idxmin()].reset_index(drop=True).rename(columns={&#39;Indicator&#39;: &#39;min&#39;, &#39;Value&#39;: &#39;B&#39;})
print(min)
#   Mode  min  B
# 0    A    1  6
# 1    B    1  7

# Create a df with the max values of &#39;Indicator&#39;, renaming the column &#39;Value&#39; to &#39;A&#39;
max = df.loc[df.groupby(&#39;Mode&#39;)[&#39;Indicator&#39;].idxmax()].reset_index(drop=True).rename(columns={&#39;Indicator&#39;: &#39;max&#39;, &#39;Value&#39;: &#39;A&#39;})
print(max)
#   Mode  max  A
# 0    A    3  2
# 1    B    4  3

# Merge the dataframes together 
result = pd.merge(min, max)

# reorder the columns to match expected output
print(result[[&#39;Mode&#39;, &#39;max&#39;,&#39;min&#39;,&#39;A&#39;, &#39;B&#39;]])
#   Mode  max  min  A  B
# 0    A    3    1  2  6
# 1    B    4    1  3  7

</details>



# 答案2
**得分**: 0

以下是代码的翻译部分:

```python
import pandas as pd

df = pd.DataFrame(
    {
        "Mode": ["A", "A", "A", "B", "B", "B", "B"],
        "Indicator": [1, 2, 3, 4, 3, 2, 1],
        "Value": [6, 5, 2, 3, 6, 8, 7],
    }
)

new_df = df.groupby("Mode")["Indicator"].agg(["max", "min"])

print(new_df)
# 输出
      max  min
Mode
A       3    1
B       4    1

以下是第二段代码的翻译:

from itertools import product

for row, (col, func) in product(["A", "B"], [("A", "max"), ("B", "min")]):
    new_df.at[row, col] = df.loc[
        (df["Mode"] == row) & (df["Indicator"] == new_df.loc[row, func]), "Value"
    ].values[0]

new_df = new_df.astype(int)

然后:

print(new_df)
# 输出
      max  min  A  B
Mode
A       3    1  2  6
B       4    1  3  7
英文:

With the dataframe you provided:

import pandas as pd


df = pd.DataFrame(
    {
        &quot;Mode&quot;: [&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;],
        &quot;Indicator&quot;: [1, 2, 3, 4, 3, 2, 1],
        &quot;Value&quot;: [6, 5, 2, 3, 6, 8, 7],
    }
)

new_df = df.groupby(&quot;Mode&quot;)[&quot;Indicator&quot;].agg([&quot;max&quot;, &quot;min&quot;])

print(new_df)
# Output
      max  min
Mode
A       3    1
B       4    1

Here is one way to do it with product from Python standard library's itertools module and Pandas at property:

from itertools import product


for row, (col, func) in product([&quot;A&quot;, &quot;B&quot;], [(&quot;A&quot;, &quot;max&quot;), (&quot;B&quot;, &quot;min&quot;)]):
    new_df.at[row, col] = df.loc[
        (df[&quot;Mode&quot;] == row) &amp; (df[&quot;Indicator&quot;] == new_df.loc[row, func]), &quot;Value&quot;
    ].values[0]

new_df = new_df.astype(int)

Then:

print(new_df)
# Output
      max  min  A  B
Mode
A       3    1  2  6
B       4    1  3  7

答案3

得分: 0

以下是您要的翻译:

The logic is unclear, there is no real reason why you would call your columns A/B since the 6/3 values in it are not coming from A/B.
逻辑不清晰,实际上没有真正的原因让你将列命名为A/B,因为其中的6/3值并不是来自A/B。

I assume you want to achieve:
我猜您想要实现以下目标:

(df.groupby('Mode')['Indicator'].agg(['idxmax', 'idxmin'])
   .rename(columns={'idxmin': 'min', 'idxmax': 'max'}).stack()
   .to_frame('x').merge(df, left_on='x', right_index=True)
   .drop(columns=['x', 'Mode']).unstack()
)

Output:
输出:

     Indicator     Value    
           max min   max min
Mode                        
A            3   1     2   6
B            4   1     3   7
C           10  10    20  20

Used input:
使用的输入数据:

  Mode  Indicator  Value
0    A          1      6
1    A          2      5
2    A          3      2
3    B          4      3
4    B          3      6
5    B          2      8
6    B          1      7
7    C         10     20

请注意,上述内容是您要求的纯翻译,不包含其他信息。

英文:

The logic is unclear, there is no real reason why you would call your columns A/B since the 6/3 values in it are not coming from A/B.

I assume you want to achieve:

(df.groupby(&#39;Mode&#39;)[&#39;Indicator&#39;].agg([&#39;idxmax&#39;, &#39;idxmin&#39;])
   .rename(columns={&#39;idxmin&#39;: &#39;min&#39;, &#39;idxmax&#39;: &#39;max&#39;}).stack()
   .to_frame(&#39;x&#39;).merge(df, left_on=&#39;x&#39;, right_index=True)
   .drop(columns=[&#39;x&#39;, &#39;Mode&#39;]).unstack()
)

Output:

     Indicator     Value    
           max min   max min
Mode                        
A            3   1     2   6
B            4   1     3   7
C           10  10    20  20

Used input:

  Mode  Indicator  Value
0    A          1      6
1    A          2      5
2    A          3      2
3    B          4      3
4    B          3      6
5    B          2      8
6    B          1      7
7    C         10     20

huangapple
  • 本文由 发表于 2023年2月18日 09:41:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定