英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论