在 Pandas 中迭代分组时去除标题和索引。

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

removing header and indexes when iterating the groups in pandas

问题

需要在迭代过程中删除每个组的标题。
我需要按几列分组,以及该组的数据框值。

  1. import pandas as pd
  2. df = pd.read_excel('D:\\Python-pandas-numpy-mat_learn\\panda-learn\\test.xlsx')
  3. # 数据帧中的列为: ['col1', 'col2', 'col3', 'col4']
  4. grp = df.groupby(by=['col1', 'col2'])
  5. for each in grp.groups:
  6. print(df[(df['col1'] == each[0]) & (df['col2'] == each[1])])
  7. # 输出如下:
  8. col1 col2 col3 col4
  9. 9 32 321 12 5mlds
  10. col1 col2 col3 col4
  11. 0 123 34 44 Row1
  12. 1 123 34 66 Row2
  13. col1 col2 col3 col4
  14. 6 214 321 3255 ere
  15. # 希望的输出如下
  16. col1 col2 col3 col4
  17. 9 32 321 12 5mlds
  18. 0 123 34 44 Row1
  19. 1 123 34 66 Row2
  20. 6 214 321 3255 ere

我不想要每个组的标题(['col1', 'col2', 'col3', 'col4'])和索引。

英文:

Need to remove header for each group while iterating.
I have requirement to group by few column and dataframe value for that group

  1. import pandas as pd
  2. df=pd.read_excel('D:\\Python-pandas-numpy-mat_learn\\panda-learn\\test.xlsx')
  3. #column in dataframes are: ['col1','col2','col3','col4']
  4. grp=df.groupby(by=['col1','col2'])
  5. for each in grp.groups:
  6. print(df[(df['col1']==each[0]) & (df['col2']==each[1])])
  7. #Output is:
  8. col1 col2 col3 col4
  9. 9 32 321 12 5mlds
  10. col1 col2 col3 col4
  11. 0 123 34 44 Row1
  12. 1 123 34 66 Row2
  13. col1 col2 col3 col4
  14. 6 214 321 3255 ere
  15. #Want output like
  16. col1 col2 col3 col4
  17. 32 321 12 5mlds
  18. 123 34 44 Row1
  19. 123 34 66 Row2
  20. 214 321 3255 ere

I don't want headers (['col1','col2','col3','col4']) and indexes for each group

答案1

得分: 0

以下是代码部分的翻译:

  1. grp=df.groupby(by=['col1','col2'])
  2. for i, (each, g) in enumerate(grp):
  3. print(g.to_string(index=False).split('\n', maxsplit=min(i,1))[-1])

输出结果:

  1. col1 col2 col3 col4
  2. 32 321 12 5mlds
  3. 123 34 44 Row1
  4. 123 34 66 Row2
  5. 214 321 3255 ere
英文:

Assuming you really need to use a loop, one option:

  1. grp=df.groupby(by=['col1','col2'])
  2. for i, (each, g) in enumerate( grp):
  3. print(g.to_string(index=False).split('\n', maxsplit=min(i,1))[-1])

Output:

  1. col1 col2 col3 col4
  2. 32 321 12 5mlds
  3. 123 34 44 Row1
  4. 123 34 66 Row2
  5. 214 321 3255 ere

huangapple
  • 本文由 发表于 2023年3月4日 03:04:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630947.html
匿名

发表评论

匿名网友

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

确定