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

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

removing header and indexes when iterating the groups in pandas

问题

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

import pandas as pd
df = pd.read_excel('D:\\Python-pandas-numpy-mat_learn\\panda-learn\\test.xlsx')
# 数据帧中的列为: ['col1', 'col2', 'col3', 'col4']
grp = df.groupby(by=['col1', 'col2'])
for each in grp.groups:
    print(df[(df['col1'] == each[0]) & (df['col2'] == each[1])])
# 输出如下:
   col1  col2  col3   col4
9    32   321    12  5mlds
   col1  col2  col3  col4
0   123    34    44  Row1
1   123    34    66  Row2
   col1  col2  col3 col4
6   214   321  3255  ere
# 希望的输出如下
   col1  col2  col3   col4
9    32   321    12  5mlds
0   123    34    44  Row1
1   123    34    66  Row2
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

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

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

答案1

得分: 0

以下是代码部分的翻译:

grp=df.groupby(by=['col1','col2'])

for i, (each, g) in enumerate(grp):
    print(g.to_string(index=False).split('\n', maxsplit=min(i,1))[-1])

输出结果:

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

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

grp=df.groupby(by=['col1','col2'])

for i, (each, g) in enumerate( grp):
    print(g.to_string(index=False).split('\n', maxsplit=min(i,1))[-1])

Output:

col1  col2  col3  col4
   32   321    12 5mlds
  123    34    44 Row1
  123    34    66 Row2
  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:

确定