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

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

groupby to show same row value from other columns

问题

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. [![enter image description here][1]][1]
  4. 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)

  1. [![enter image description here][2]][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.)
  3. [1]: https://i.stack.imgur.com/bl63Q.png
  4. [2]: https://i.stack.imgur.com/2Ynod.png
  5. </details>
  6. # 答案1
  7. **得分**: 1
  8. 您可以在两个步骤中完成,使用`groupby``idxmin()``idxmax()`
  9. ```python
  10. # 创建一个包含'Indicator'的最小值的DataFrame,将列名'Value'重命名为'B'
  11. min = df.loc[df.groupby('Mode')['Indicator'].idxmin()].reset_index(drop=True).rename(columns={'Indicator': 'min', 'Value': 'B'})
  12. print(min)
  13. # Mode min B
  14. # 0 A 1 6
  15. # 1 B 1 7
  16. # 创建一个包含'Indicator'的最大值的DataFrame,将列名'Value'重命名为'A'
  17. max = df.loc[df.groupby('Mode')['Indicator'].idxmax()].reset_index(drop=True).rename columns={'Indicator': 'max', 'Value': 'A'})
  18. print(max)
  19. # Mode max A
  20. # 0 A 3 2
  21. # 1 B 4 3
  22. # 合并这两个DataFrame
  23. result = pd.merge(min, max)
  24. # 重新排列列以匹配期望的输出
  25. print(result[['Mode', 'max', 'min', 'A', 'B']])
  26. # Mode max min A B
  27. # 0 A 3 1 2 6
  28. # 1 B 4 1 3 7
英文:

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

  1. # Create a df with the min values of &#39;Indicator&#39;, renaming the column &#39;Value&#39; to &#39;B&#39;
  2. 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;})
  3. print(min)
  4. # Mode min B
  5. # 0 A 1 6
  6. # 1 B 1 7
  7. # Create a df with the max values of &#39;Indicator&#39;, renaming the column &#39;Value&#39; to &#39;A&#39;
  8. 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;})
  9. print(max)
  10. # Mode max A
  11. # 0 A 3 2
  12. # 1 B 4 3
  13. # Merge the dataframes together
  14. result = pd.merge(min, max)
  15. # reorder the columns to match expected output
  16. print(result[[&#39;Mode&#39;, &#39;max&#39;,&#39;min&#39;,&#39;A&#39;, &#39;B&#39;]])
  17. # Mode max min A B
  18. # 0 A 3 1 2 6
  19. # 1 B 4 1 3 7
  20. </details>
  21. # 答案2
  22. **得分**: 0
  23. 以下是代码的翻译部分:
  24. ```python
  25. import pandas as pd
  26. df = pd.DataFrame(
  27. {
  28. "Mode": ["A", "A", "A", "B", "B", "B", "B"],
  29. "Indicator": [1, 2, 3, 4, 3, 2, 1],
  30. "Value": [6, 5, 2, 3, 6, 8, 7],
  31. }
  32. )
  33. new_df = df.groupby("Mode")["Indicator"].agg(["max", "min"])
  34. print(new_df)
  35. # 输出
  36. max min
  37. Mode
  38. A 3 1
  39. B 4 1

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

  1. from itertools import product
  2. for row, (col, func) in product(["A", "B"], [("A", "max"), ("B", "min")]):
  3. new_df.at[row, col] = df.loc[
  4. (df["Mode"] == row) & (df["Indicator"] == new_df.loc[row, func]), "Value"
  5. ].values[0]
  6. new_df = new_df.astype(int)

然后:

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

With the dataframe you provided:

  1. import pandas as pd
  2. df = pd.DataFrame(
  3. {
  4. &quot;Mode&quot;: [&quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;],
  5. &quot;Indicator&quot;: [1, 2, 3, 4, 3, 2, 1],
  6. &quot;Value&quot;: [6, 5, 2, 3, 6, 8, 7],
  7. }
  8. )
  9. new_df = df.groupby(&quot;Mode&quot;)[&quot;Indicator&quot;].agg([&quot;max&quot;, &quot;min&quot;])
  10. print(new_df)
  11. # Output
  12. max min
  13. Mode
  14. A 3 1
  15. B 4 1

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

  1. from itertools import product
  2. for row, (col, func) in product([&quot;A&quot;, &quot;B&quot;], [(&quot;A&quot;, &quot;max&quot;), (&quot;B&quot;, &quot;min&quot;)]):
  3. new_df.at[row, col] = df.loc[
  4. (df[&quot;Mode&quot;] == row) &amp; (df[&quot;Indicator&quot;] == new_df.loc[row, func]), &quot;Value&quot;
  5. ].values[0]
  6. new_df = new_df.astype(int)

Then:

  1. print(new_df)
  2. # Output
  3. max min A B
  4. Mode
  5. A 3 1 2 6
  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:
我猜您想要实现以下目标:

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

Output:
输出:

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

Used input:
使用的输入数据:

  1. Mode Indicator Value
  2. 0 A 1 6
  3. 1 A 2 5
  4. 2 A 3 2
  5. 3 B 4 3
  6. 4 B 3 6
  7. 5 B 2 8
  8. 6 B 1 7
  9. 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:

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

Output:

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

Used input:

  1. Mode Indicator Value
  2. 0 A 1 6
  3. 1 A 2 5
  4. 2 A 3 2
  5. 3 B 4 3
  6. 4 B 3 6
  7. 5 B 2 8
  8. 6 B 1 7
  9. 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:

确定