简体   繁体   English

有条件地将单元格的内容合并到列中

[英]conditionally merge cells' contents in a column

Looking for a pandanic way to turn the following df: 寻找一种轻松的方法来打开以下df:

    name    desc
0   A       a
1   NaN     aa
2   NaN     aaa
3   B       b
4   NaN     bb

into: 成:

    name    desc
0   A       a
            aa
            aaa
3   B       b
            bb

# strings in desc are concat-ed together with end of line char

I am thinking of the general directions of either itertuple or backfill+groupby, but both of those approaches require some juggling. 我正在考虑itertuple或backfill + groupby的一般方向,但是这两种方法都需要进行一些调整。

here is the starting point: 这是起点:

import pandas as pd
import numpy as np
nan = np.nan

df = pd.DataFrame(
    {'name': ['A', nan, nan, 'B', nan],
    'desc': ['a', 'aa', 'aaa', 'b', 'bb']}
)

you can call ffill directly and agg without using apply and lambda 您可以直接调用ffillagg而无需使用applylambda

In [719]: df.ffill().groupby('name').agg('\n'.join).reset_index()
Out[719]:
  name        desc
0    A  a\naa\naaa
1    B       b\nbb

or: 要么:

In [729]: df.ffill().groupby('name', as_index=False).agg({'desc': '\n'.join})
Out[729]:
  name        desc
0    A  a\naa\naaa
1    B       b\nbb

I think you want a combination of fillna(method='ffill') and groupby . 我认为您想要fillna(method='ffill')groupby

How does this look? 看起来如何?

import pandas as pd
import numpy as np
nan = np.nan

df = pd.DataFrame(
    {'name': ['A', nan, nan, 'B', nan],
    'desc': ['a', 'aa', 'aaa', 'b', 'bb']}
)

df['name'] = df['name'].fillna(method='ffill')

df = df.groupby('name')['desc'].apply(lambda d: '\n'.join(d)).reset_index()
print df

prints 版画

  name        desc
0    A  a\naa\naaa
1    B       b\nbb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有条件地修改熊猫列中的细胞? - How to modify cells in column conditionally in pandas? 如何合并两个数据框并有条件地合并一列 - How do you merge two dataframes and conditionally merge one column 熊猫-基于两个单元格的相似内容合并行 - Pandas - Merge rows based on similar contents of two cells 如果两列中的值相同,则合并熊猫中的单元格 - Merge cells in pandas if values in two column is same Pandas 合并具有相同值的第一列中的单元格 - Pandas Merge Cells in First Column with Same Values Openpyxl在一列中合并具有相同值的单元格 - Openpyxl Merge cells with same value in one column 根据另一个数据框中的列有条件地格式化每列中的单元格 - Conditionally format cells in each column based on columns in another dataframe 有条件地附加文件内容 - Conditionally append file contents 当列为一系列列表时,如何有条件地将其添加到pandas数据框列中的单元格选择中? - How do I add conditionally to a selection of cells in a pandas dataframe column when the the column is a series of lists? Python-docx - 使用一个命令合并表格的行或列(或列中的特定单元格子集)中的所有单元格 - Python-docx - merge ALL cells in a row or column of a table (or a specific subset of cells in a column) with one command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM