【问题标题】:Split (explode) pandas dataframe string entry to separate rows拆分(分解)熊猫数据框字符串条目以分隔行
【发布时间】:2022-01-20 20:24:02
【问题描述】:

我有一个pandas dataframe,其中一列文本字符串包含逗号分隔的值。我想拆分每个 CSV 字段并为每个条目创建一个新行(假设 CSV 是干净的,只需要在“,”上拆分)。比如a应该变成b

In [7]: a
Out[7]: 
    var1  var2
0  a,b,c     1
1  d,e,f     2

In [8]: b
Out[8]: 
  var1  var2
0    a     1
1    b     1
2    c     1
3    d     2
4    e     2
5    f     2

到目前为止,我已经尝试了各种简单的功能,但是.apply 方法在轴上使用时似乎只接受一行作为返回值,我无法让.transform 工作。任何建议将不胜感激!

示例数据:

from pandas import DataFrame
import numpy as np
a = DataFrame([{'var1': 'a,b,c', 'var2': 1},
               {'var1': 'd,e,f', 'var2': 2}])
b = DataFrame([{'var1': 'a', 'var2': 1},
               {'var1': 'b', 'var2': 1},
               {'var1': 'c', 'var2': 1},
               {'var1': 'd', 'var2': 2},
               {'var1': 'e', 'var2': 2},
               {'var1': 'f', 'var2': 2}])

我知道这行不通,因为我们通过 numpy 丢失了 DataFrame 元数据,但它应该让您了解我试图做什么:

def fun(row):
    letters = row['var1']
    letters = letters.split(',')
    out = np.array([row] * len(letters))
    out['var1'] = letters
a['idx'] = range(a.shape[0])
z = a.groupby('idx')
z.transform(fun)

【问题讨论】:

标签: python pandas numpy dataframe


【解决方案1】:

更新 3: 使用 Series.explode() / DataFrame.explode() methods(在 Pandas 0.25.0 中实现并在 Pandas 1.3.0 中扩展以支持多列分解)更有意义,如使用示例所示:

对于单列:

In [1]: df = pd.DataFrame({'A': [[0, 1, 2], 'foo', [], [3, 4]],
   ...:                    'B': 1,
   ...:                    'C': [['a', 'b', 'c'], np.nan, [], ['d', 'e']]})

In [2]: df
Out[2]:
           A  B          C
0  [0, 1, 2]  1  [a, b, c]
1        foo  1        NaN
2         []  1         []
3     [3, 4]  1     [d, e]

In [3]: df.explode('A')
Out[3]:
     A  B          C
0    0  1  [a, b, c]
0    1  1  [a, b, c]
0    2  1  [a, b, c]
1  foo  1        NaN
2  NaN  1         []
3    3  1     [d, e]
3    4  1     [d, e]

对于多列(对于 Pandas 1.3.0+):

In [4]: df.explode(['A', 'C'])
Out[4]:
     A  B    C
0    0  1    a
0    1  1    b
0    2  1    c
1  foo  1  NaN
2  NaN  1  NaN
3    3  1    d
3    4  1    e

更新 2: 更通用的矢量化函数,适用于多个 normal 和多个 list

def explode(df, lst_cols, fill_value='', preserve_index=False):
    # make sure `lst_cols` is list-alike
    if (lst_cols is not None
        and len(lst_cols) > 0
        and not isinstance(lst_cols, (list, tuple, np.ndarray, pd.Series))):
        lst_cols = [lst_cols]
    # all columns except `lst_cols`
    idx_cols = df.columns.difference(lst_cols)
    # calculate lengths of lists
    lens = df[lst_cols[0]].str.len()
    # preserve original index values    
    idx = np.repeat(df.index.values, lens)
    # create "exploded" DF
    res = (pd.DataFrame({
                col:np.repeat(df[col].values, lens)
                for col in idx_cols},
                index=idx)
             .assign(**{col:np.concatenate(df.loc[lens>0, col].values)
                            for col in lst_cols}))
    # append those rows that have empty lists
    if (lens == 0).any():
        # at least one list in cells is empty
        res = (res.append(df.loc[lens==0, idx_cols], sort=False)
                  .fillna(fill_value))
    # revert the original index order
    res = res.sort_index()
    # reset index if requested
    if not preserve_index:        
        res = res.reset_index(drop=True)
    return res

演示:

多个list 列 - 所有list 列必须在每行中具有相同的元素数:

In [134]: df
Out[134]:
   aaa  myid        num          text
0   10     1  [1, 2, 3]  [aa, bb, cc]
1   11     2         []            []
2   12     3     [1, 2]      [cc, dd]
3   13     4         []            []

In [135]: explode(df, ['num','text'], fill_value='')
Out[135]:
   aaa  myid num text
0   10     1   1   aa
1   10     1   2   bb
2   10     1   3   cc
3   11     2
4   12     3   1   cc
5   12     3   2   dd
6   13     4

保留原始索引值:

In [136]: explode(df, ['num','text'], fill_value='', preserve_index=True)
Out[136]:
   aaa  myid num text
0   10     1   1   aa
0   10     1   2   bb
0   10     1   3   cc
1   11     2
2   12     3   1   cc
2   12     3   2   dd
3   13     4

设置:

df = pd.DataFrame({
 'aaa': {0: 10, 1: 11, 2: 12, 3: 13},
 'myid': {0: 1, 1: 2, 2: 3, 3: 4},
 'num': {0: [1, 2, 3], 1: [], 2: [1, 2], 3: []},
 'text': {0: ['aa', 'bb', 'cc'], 1: [], 2: ['cc', 'dd'], 3: []}
})

CSV 列:

In [46]: df
Out[46]:
        var1  var2 var3
0      a,b,c     1   XX
1  d,e,f,x,y     2   ZZ

In [47]: explode(df.assign(var1=df.var1.str.split(',')), 'var1')
Out[47]:
  var1  var2 var3
0    a     1   XX
1    b     1   XX
2    c     1   XX
3    d     2   ZZ
4    e     2   ZZ
5    f     2   ZZ
6    x     2   ZZ
7    y     2   ZZ

使用这个小技巧,我们可以将类似 CSV 的列转换为 list 列:

In [48]: df.assign(var1=df.var1.str.split(','))
Out[48]:
              var1  var2 var3
0        [a, b, c]     1   XX
1  [d, e, f, x, y]     2   ZZ

更新: 通用矢量化方法(也适用于多列):

原始 DF:

In [177]: df
Out[177]:
        var1  var2 var3
0      a,b,c     1   XX
1  d,e,f,x,y     2   ZZ

解决方案:

首先让我们将 CSV 字符串转换为列表:

In [178]: lst_col = 'var1' 

In [179]: x = df.assign(**{lst_col:df[lst_col].str.split(',')})

In [180]: x
Out[180]:
              var1  var2 var3
0        [a, b, c]     1   XX
1  [d, e, f, x, y]     2   ZZ

现在我们可以这样做了:

In [181]: pd.DataFrame({
     ...:     col:np.repeat(x[col].values, x[lst_col].str.len())
     ...:     for col in x.columns.difference([lst_col])
     ...: }).assign(**{lst_col:np.concatenate(x[lst_col].values)})[x.columns.tolist()]
     ...:
Out[181]:
  var1  var2 var3
0    a     1   XX
1    b     1   XX
2    c     1   XX
3    d     2   ZZ
4    e     2   ZZ
5    f     2   ZZ
6    x     2   ZZ
7    y     2   ZZ

旧答案:

@AFinkelstein solution 的启发,我想让它更通用,可以应用于具有多于两列的 DF,并且与 AFinkelstein 的解决方案一样快,几乎一样快):

In [2]: df = pd.DataFrame(
   ...:    [{'var1': 'a,b,c', 'var2': 1, 'var3': 'XX'},
   ...:     {'var1': 'd,e,f,x,y', 'var2': 2, 'var3': 'ZZ'}]
   ...: )

In [3]: df
Out[3]:
        var1  var2 var3
0      a,b,c     1   XX
1  d,e,f,x,y     2   ZZ

In [4]: (df.set_index(df.columns.drop('var1',1).tolist())
   ...:    .var1.str.split(',', expand=True)
   ...:    .stack()
   ...:    .reset_index()
   ...:    .rename(columns={0:'var1'})
   ...:    .loc[:, df.columns]
   ...: )
Out[4]:
  var1  var2 var3
0    a     1   XX
1    b     1   XX
2    c     1   XX
3    d     2   ZZ
4    e     2   ZZ
5    f     2   ZZ
6    x     2   ZZ
7    y     2   ZZ

【讨论】:

  • 老兄,如果你可以在 Git pandas 中打开讨论,我认为我们确实需要这样的内置函数!!!我已经看到很多关于在 SO for pandas 中取消列表和取消嵌套的问题
  • 如何将其用于多列。就像我在 2 列中有逗号分隔的数据并想按顺序执行一样?
  • 不幸的是,如果您的列表元素是元组,它就不起作用。但是在将整个元组转换为字符串之后,它就像一个魅力!
  • 看来文本的恳求被熊猫大神们听到了,他们在API中安装了.explode()方法(另见this answer)。
【解决方案2】:

经过痛苦的实验以找到比公认答案更快的东西,我得到了这个工作。在我尝试过的数据集上,它的运行速度提高了大约 100 倍。

如果有人知道如何使它更优雅,请务必修改我的代码。如果不将要保留的其他列设置为索引,然后重置索引并重新命名列,我找不到可行的方法,但我想还有其他方法可行。

b = DataFrame(a.var1.str.split(',').tolist(), index=a.var2).stack()
b = b.reset_index()[[0, 'var2']] # var1 variable is currently labeled 0
b.columns = ['var1', 'var2'] # renaming var1

【讨论】:

  • 此解决方案的运行速度明显加快,而且似乎使用的内存更少,
  • 这是一个很好的矢量化熊猫解决方案,我一直在寻找它。谢谢!
  • 当我在自己的数据集上尝试此操作时,我在第一步中不断收到TypeError: object of type 'float' has no len() (DataFrame(df.var1.str.split(',').tolist()))
  • @user5359531 您的数据集可能在该列中有一些NaN,因此替换为b = DataFrame(a.var1.str.split(',').values.tolist(), index=a.var2).stack()
  • 仅供参考 here's 用示例很好地描述了这个解决方案。
【解决方案3】:

熊猫 >= 0.25

Series 和 DataFrame 方法定义了一个 .explode() 方法,该方法将 列表 分解为单独的行。请参阅Exploding a list-like column 上的文档部分。

因为你有一个逗号分隔的字符串列表,所以用逗号分割字符串以获得元素列表,然后在该列上调用explode

df = pd.DataFrame({'var1': ['a,b,c', 'd,e,f'], 'var2': [1, 2]})
df
    var1  var2
0  a,b,c     1
1  d,e,f     2

df.assign(var1=df['var1'].str.split(',')).explode('var1')

  var1  var2
0    a     1
0    b     1
0    c     1
1    d     2
1    e     2
1    f     2

请注意,explode 仅适用于单个列(目前)。要一次分解多个列,请参见下文。

NaN 和空列表可以得到它们应得的待遇,而无需您费尽心思才能做到正确。

df = pd.DataFrame({'var1': ['d,e,f', '', np.nan], 'var2': [1, 2, 3]})
df
    var1  var2
0  d,e,f     1
1            2
2    NaN     3

df['var1'].str.split(',')

0    [d, e, f]
1           []
2          NaN

df.assign(var1=df['var1'].str.split(',')).explode('var1')

  var1  var2
0    d     1
0    e     1
0    f     1
1          2  # empty list entry becomes empty string after exploding 
2  NaN     3  # NaN left un-touched

与基于 ravel/repeat 的解决方案相比,这是一个重要的优势(完全忽略空列表,并阻塞 NaN)。


分解多列

请注意,explode 一次只能处理一列,但您可以使用 apply 一次分解多列:

df = pd.DataFrame({'var1': ['a,b,c', 'd,e,f'], 
                   'var2': ['i,j,k', 'l,m,n'], 
                   'var3': [1, 2]})
df
    var1   var2  var3
0  a,b,c  i,j,k     1
1  d,e,f  l,m,n     2

(df.set_index(['var3']) 
   .apply(lambda col: col.str.split(',').explode())
   .reset_index()
   .reindex(df.columns, axis=1))

df
  var1 var2  var3
0    a    i     1
1    b    j     1
2    c    k     1
3    d    l     2
4    e    m     2
5    f    n     2

想法是将所有应该分解的列设置为索引,然后通过apply 分解剩余的列。当列表大小相同时,这很有效。

【讨论】:

  • +1。但是当我使用新列时,它没有按预期工作。喜欢df.assign(var3=df['var1'].str.split(',')).explode('var1') 你能帮忙吗?我用var3替换了var1
  • @Avinash 要爆炸的参数也应该是 var3。这是一个基本问题,因此请花几分钟时间了解原因。
  • 如果你有带字符串和整数的行,你需要 .astype(str),否则你会得到整数的 NaN 值。
  • 我不知道为什么这个简单易读的解决方案不是票数最高的答案,而是一个复杂、难以理解的答案。在我的情况下,我所要做的就是像df = df.assign(var1=df['var1'].str.split(',')).explode('var1') 那样分配返回值。为了便于阅读,我还将这个过程分成了多行。
  • 注意:如果你想继续使用这个展开的DataFrame,那么你需要明确地将它分配给它自己,即df = df.explode('var1')。这不支持就地函数调用
【解决方案4】:

这样的事情怎么样:

In [55]: pd.concat([Series(row['var2'], row['var1'].split(','))              
                    for _, row in a.iterrows()]).reset_index()
Out[55]: 
  index  0
0     a  1
1     b  1
2     c  1
3     d  2
4     e  2
5     f  2

然后你只需要重命名列

【讨论】:

  • 看起来这行得通。谢谢你的帮助!不过,一般来说,有没有一种首选的拆分-应用-组合方法,其中 Apply 返回任意大小的数据帧(但对于所有块都是一致的),而 Combine 只是 vstacks 返回的 DF?
  • 大家好。很抱歉这么晚才加入,但想知道是否没有更好的解决方案。我第一次尝试使用 iterrows,因为这似乎是门票。我也对提出的解决方案感到困惑。 “_”代表什么?你能解释一下解决方案是如何工作的吗? --谢谢
  • 解决方案可以扩展到两列以上吗?
  • 为什么这有时会起作用(在某些数据帧上)但似乎不适用于其他数据帧?我已经让它在一个数据集上工作,但是现在在另一个数据集上尝试时,我得到“NameError:name 'Series' is not defined
【解决方案5】:

这里有一个function I wrote 用于此常见任务。它比Series/stack 方法更有效。保留列顺序和名称。

def tidy_split(df, column, sep='|', keep=False):
    """
    Split the values of a column and expand so the new DataFrame has one split
    value per row. Filters rows where the column is missing.

    Params
    ------
    df : pandas.DataFrame
        dataframe with the column to split and expand
    column : str
        the column to split and expand
    sep : str
        the string used to split the column's values
    keep : bool
        whether to retain the presplit value as it's own row

    Returns
    -------
    pandas.DataFrame
        Returns a dataframe with the same columns as `df`.
    """
    indexes = list()
    new_values = list()
    df = df.dropna(subset=[column])
    for i, presplit in enumerate(df[column].astype(str)):
        values = presplit.split(sep)
        if keep and len(values) > 1:
            indexes.append(i)
            new_values.append(presplit)
        for value in values:
            indexes.append(i)
            new_values.append(value)
    new_df = df.iloc[indexes, :].copy()
    new_df[column] = new_values
    return new_df

有了这个函数,original question就这么简单:

tidy_split(a, 'var1', sep=',')

【讨论】:

  • 这速度非常快!非常感谢。
【解决方案6】:

类似问题:pandas: How do I split text in a column into multiple rows?

你可以这样做:

>> a=pd.DataFrame({"var1":"a,b,c d,e,f".split(),"var2":[1,2]})
>> s = a.var1.str.split(",").apply(pd.Series, 1).stack()
>> s.index = s.index.droplevel(-1)
>> del a['var1']
>> a.join(s)
   var2 var1
0     1    a
0     1    b
0     1    c
1     2    d
1     2    e
1     2    f

【讨论】:

  • 再添加一个重命名代码s.name = 'var1' 后生效
【解决方案7】:

有可能在不改变数据帧结构的情况下拆分和分解数据帧

拆分和扩展特定列的数据

输入:

    var1    var2
0   a,b,c   1
1   d,e,f   2



#Get the indexes which are repetative with the split 
df['var1'] = df['var1'].str.split(',')
df = df.explode('var1')

输出:

    var1    var2
0   a   1
0   b   1
0   c   1
1   d   2
1   e   2
1   f   2

Edit-1

为多列拆分和扩展行

Filename    RGB                                             RGB_type
0   A   [[0, 1650, 6, 39], [0, 1691, 1, 59], [50, 1402...   [r, g, b]
1   B   [[0, 1423, 16, 38], [0, 1445, 16, 46], [0, 141...   [r, g, b]

根据引用列重新索引,将列值信息与栈对齐

df = df.reindex(df.index.repeat(df['RGB_type'].apply(len)))
df = df.groupby('Filename').apply(lambda x:x.apply(lambda y: pd.Series(y.iloc[0])))
df.reset_index(drop=True).ffill()

输出:

                Filename    RGB_type    Top 1 colour    Top 1 frequency Top 2 colour    Top 2 frequency
    Filename                            
 A  0       A   r   0   1650    6   39
    1       A   g   0   1691    1   59
    2       A   b   50  1402    49  187
 B  0       B   r   0   1423    16  38
    1       B   g   0   1445    16  46
    2       B   b   0   1419    16  39

【讨论】:

    【解决方案8】:

    TL;DR

    import pandas as pd
    import numpy as np
    
    def explode_str(df, col, sep):
        s = df[col]
        i = np.arange(len(s)).repeat(s.str.count(sep) + 1)
        return df.iloc[i].assign(**{col: sep.join(s).split(sep)})
    
    def explode_list(df, col):
        s = df[col]
        i = np.arange(len(s)).repeat(s.str.len())
        return df.iloc[i].assign(**{col: np.concatenate(s)})
    

    演示

    explode_str(a, 'var1', ',')
    
      var1  var2
    0    a     1
    0    b     1
    0    c     1
    1    d     2
    1    e     2
    1    f     2
    

    让我们创建一个包含列表的新数据框d

    d = a.assign(var1=lambda d: d.var1.str.split(','))
    
    explode_list(d, 'var1')
    
      var1  var2
    0    a     1
    0    b     1
    0    c     1
    1    d     2
    1    e     2
    1    f     2
    

    一般评论

    我将使用np.arangerepeat 来生成可以与iloc 一起使用的数据帧索引位置。

    常见问题

    我为什么不用loc

    因为索引可能不是唯一的,并且使用loc 将返回与查询索引匹配的每一行。

    为什么不使用values 属性并对其进行切片?

    当调用values 时,如果整个数据帧都在一个内聚的“块”中,Pandas 将返回一个数组视图,即“块”。否则 Pandas 将不得不拼凑一个新数组。拼凑时,该数组必须是统一的 dtype。这通常意味着返回一个 dtype 为object 的数组。通过使用iloc 而不是对values 属性进行切片,我不必处理这些问题。

    你为什么使用assign

    当我使用 assign 并使用我正在爆炸的相同列名时,我会覆盖现有列并保持其在数据框中的位置。

    为什么索引值会重复?

    通过在重复位置上使用iloc,生成的索引显示相同的重复模式。列表或字符串的每个元素重复一次。
    这可以用reset_index(drop=True)重置


    对于字符串

    我不想过早地拆分字符串。因此,我计算 sep 参数的出现次数,假设如果我要拆分,结果列表的长度将比分隔符的数量多一。

    然后我使用 sepjoin 的字符串,然后使用 split

    def explode_str(df, col, sep):
        s = df[col]
        i = np.arange(len(s)).repeat(s.str.count(sep) + 1)
        return df.iloc[i].assign(**{col: sep.join(s).split(sep)})
    

    对于列表

    与字符串类似,但我不需要计算 sep 的出现次数,因为它已经分裂了。

    我使用 Numpy 的 concatenate 将列表挤在一起。

    import pandas as pd
    import numpy as np
    
    def explode_list(df, col):
        s = df[col]
        i = np.arange(len(s)).repeat(s.str.len())
        return df.iloc[i].assign(**{col: np.concatenate(s)})
    

    【讨论】:

    • 我喜欢这个。真的很简洁,性能也应该很好。但是有一个问题: df.iloc[i] 是否与重复数据帧的行相同,还是比这更有效?谢谢!
    【解决方案9】:

    我为具有任意列数的数据框提出了一个解决方案(同时仍然一次只分隔一个列的条目)。

    def splitDataFrameList(df,target_column,separator):
        ''' df = dataframe to split,
        target_column = the column containing the values to split
        separator = the symbol used to perform the split
    
        returns: a dataframe with each entry for the target column separated, with each element moved into a new row. 
        The values in the other columns are duplicated across the newly divided rows.
        '''
        def splitListToRows(row,row_accumulator,target_column,separator):
            split_row = row[target_column].split(separator)
            for s in split_row:
                new_row = row.to_dict()
                new_row[target_column] = s
                row_accumulator.append(new_row)
        new_rows = []
        df.apply(splitListToRows,axis=1,args = (new_rows,target_column,separator))
        new_df = pandas.DataFrame(new_rows)
        return new_df
    

    【讨论】:

    • 不错,但是由于这个 todict() 转换很慢:(
    【解决方案10】:

    这是一个相当简单的消息,它使用 pandas str 访问器中的 split 方法,然后使用 NumPy 将每一行展平为一个数组。

    通过使用np.repeat 将非拆分列重复正确的次数来检索相应的值。

    var1 = df.var1.str.split(',', expand=True).values.ravel()
    var2 = np.repeat(df.var2.values, len(var1) / len(df))
    
    pd.DataFrame({'var1': var1,
                  'var2': var2})
    
      var1  var2
    0    a     1
    1    b     1
    2    c     1
    3    d     2
    4    e     2
    5    f     2
    

    【讨论】:

    • 这可能是一个非常漂亮的答案。不幸的是,它不能扩展到很多列,是吗?
    【解决方案11】:

    我一直在努力解决内存不足的问题,我使用各种方法来扩展我的列表,因此我准备了一些基准来帮助我决定要投票的答案。我测试了五个场景,列表长度与列表数量的比例不同。分享以下结果:

    时间:(越少越好,点击查看大图)

    内存使用峰值:(越少越好)

    结论

    • @MaxU's answer(更新 2),代号 concatenate 几乎在所有情况下都提供了最佳速度,同时保持低内存使用率,
    • 如果您需要处理具有相对较小列表的大量行并且可以承受增加的峰值内存,请参阅@DMulligan's answer(代号堆栈),
    • 接受的@Chang's answer 适用于具有几行但列表非常大的数据框。

    完整的详细信息(功能和基准测试代码)在此GitHub gist 中。请注意,基准问题已简化,不包括将字符串拆分到列表中 - 大多数解决方案都以类似的方式执行。

    【讨论】:

    【解决方案12】:

    使用split(___, expand=True)levelname 参数到reset_index() 的单线:

    >>> b = a.var1.str.split(',', expand=True).set_index(a.var2).stack().reset_index(level=0, name='var1')
    >>> b
       var2 var1
    0     1    a
    1     1    b
    2     1    c
    0     2    d
    1     2    e
    2     2    f
    

    如果您需要b 看起来与问题中的完全一样,您还可以这样做:

    >>> b = b.reset_index(drop=True)[['var1', 'var2']]
    >>> b
      var1  var2
    0    a     1
    1    b     1
    2    c     1
    3    d     2
    4    e     2
    5    f     2
    

    【讨论】:

      【解决方案13】:

      基于优秀的@DMulligan 的solution,这是一个通用的矢量化(无循环)函数,它将数据帧的一列拆分为多行,并将其合并回原始数据帧。它还使用了来自这个answer 的一个很棒的通用change_column_order 函数。

      def change_column_order(df, col_name, index):
          cols = df.columns.tolist()
          cols.remove(col_name)
          cols.insert(index, col_name)
          return df[cols]
      
      def split_df(dataframe, col_name, sep):
          orig_col_index = dataframe.columns.tolist().index(col_name)
          orig_index_name = dataframe.index.name
          orig_columns = dataframe.columns
          dataframe = dataframe.reset_index()  # we need a natural 0-based index for proper merge
          index_col_name = (set(dataframe.columns) - set(orig_columns)).pop()
          df_split = pd.DataFrame(
              pd.DataFrame(dataframe[col_name].str.split(sep).tolist())
              .stack().reset_index(level=1, drop=1), columns=[col_name])
          df = dataframe.drop(col_name, axis=1)
          df = pd.merge(df, df_split, left_index=True, right_index=True, how='inner')
          df = df.set_index(index_col_name)
          df.index.name = orig_index_name
          # merge adds the column to the last place, so we need to move it back
          return change_column_order(df, col_name, orig_col_index)
      

      例子:

      df = pd.DataFrame([['a:b', 1, 4], ['c:d', 2, 5], ['e:f:g:h', 3, 6]], 
                        columns=['Name', 'A', 'B'], index=[10, 12, 13])
      df
              Name    A   B
          10   a:b     1   4
          12   c:d     2   5
          13   e:f:g:h 3   6
      
      split_df(df, 'Name', ':')
          Name    A   B
      10   a       1   4
      10   b       1   4
      12   c       2   5
      12   d       2   5
      13   e       3   6
      13   f       3   6    
      13   g       3   6    
      13   h       3   6    
      

      请注意,它保留了列的原始索引和顺序。它也适用于具有非顺序索引的数据帧。

      【讨论】:

      【解决方案14】:

      字符串函数 split 可以接受一个选项布尔参数 'expand'。

      这是使用此参数的解决方案:

      (a.var1
        .str.split(",",expand=True)
        .set_index(a.var2)
        .stack()
        .reset_index(level=1, drop=True)
        .reset_index()
        .rename(columns={0:"var1"}))
      

      【讨论】:

        【解决方案15】:

        我真的很欣赏“Chang She”的回答,但是iterrows() 函数在大型数据集上需要很长时间。我遇到了这个问题,我来到了这个。

        # First, reset_index to make the index a column
        a = a.reset_index().rename(columns={'index':'duplicated_idx'})
        
        # Get a longer series with exploded cells to rows
        series = pd.DataFrame(a['var1'].str.split('/')
                              .tolist(), index=a.duplicated_idx).stack()
        
        # New df from series and merge with the old one
        b = series.reset_index([0, 'duplicated_idx'])
        b = b.rename(columns={0:'var1'})
        
        # Optional & Advanced: In case, there are other columns apart from var1 & var2
        b.merge(
            a[a.columns.difference(['var1'])],
            on='duplicated_idx')
        
        # Optional: Delete the "duplicated_index"'s column, and reorder columns
        b = b[a.columns.difference(['duplicated_idx'])]
        

        【讨论】:

          【解决方案16】:

          刚刚从上面使用了 jiln 的优秀答案,但需要扩展以拆分多个列。以为我会分享。

          def splitDataFrameList(df,target_column,separator):
          ''' df = dataframe to split,
          target_column = the column containing the values to split
          separator = the symbol used to perform the split
          
          returns: a dataframe with each entry for the target column separated, with each element moved into a new row. 
          The values in the other columns are duplicated across the newly divided rows.
          '''
          def splitListToRows(row, row_accumulator, target_columns, separator):
              split_rows = []
              for target_column in target_columns:
                  split_rows.append(row[target_column].split(separator))
              # Seperate for multiple columns
              for i in range(len(split_rows[0])):
                  new_row = row.to_dict()
                  for j in range(len(split_rows)):
                      new_row[target_columns[j]] = split_rows[j][i]
                  row_accumulator.append(new_row)
          new_rows = []
          df.apply(splitListToRows,axis=1,args = (new_rows,target_column,separator))
          new_df = pd.DataFrame(new_rows)
          return new_df
          

          【讨论】:

            【解决方案17】:

            通过 MultiIndex 支持升级了 MaxU 的答案

            def explode(df, lst_cols, fill_value='', preserve_index=False):
                """
                usage:
                    In [134]: df
                    Out[134]:
                       aaa  myid        num          text
                    0   10     1  [1, 2, 3]  [aa, bb, cc]
                    1   11     2         []            []
                    2   12     3     [1, 2]      [cc, dd]
                    3   13     4         []            []
            
                    In [135]: explode(df, ['num','text'], fill_value='')
                    Out[135]:
                       aaa  myid num text
                    0   10     1   1   aa
                    1   10     1   2   bb
                    2   10     1   3   cc
                    3   11     2
                    4   12     3   1   cc
                    5   12     3   2   dd
                    6   13     4
                """
                # make sure `lst_cols` is list-alike
                if (lst_cols is not None
                    and len(lst_cols) > 0
                    and not isinstance(lst_cols, (list, tuple, np.ndarray, pd.Series))):
                    lst_cols = [lst_cols]
                # all columns except `lst_cols`
                idx_cols = df.columns.difference(lst_cols)
                # calculate lengths of lists
                lens = df[lst_cols[0]].str.len()
                # preserve original index values    
                idx = np.repeat(df.index.values, lens)
                res = (pd.DataFrame({
                            col:np.repeat(df[col].values, lens)
                            for col in idx_cols},
                            index=idx)
                         .assign(**{col:np.concatenate(df.loc[lens>0, col].values)
                                        for col in lst_cols}))
                # append those rows that have empty lists
                if (lens == 0).any():
                    # at least one list in cells is empty
                    res = (res.append(df.loc[lens==0, idx_cols], sort=False)
                              .fillna(fill_value))
                # revert the original index order
                res = res.sort_index()
                # reset index if requested
                if not preserve_index:        
                    res = res.reset_index(drop=True)
            
                # if original index is MultiIndex build the dataframe from the multiindex
                # create "exploded" DF
                if isinstance(df.index, pd.MultiIndex):
                    res = res.reindex(
                        index=pd.MultiIndex.from_tuples(
                            res.index,
                            names=['number', 'color']
                        )
                )
                return res
            

            【讨论】:

              【解决方案18】:

              我要添加到此集合的解决方案版本! :-)

              # Original problem
              from pandas import DataFrame
              import numpy as np
              a = DataFrame([{'var1': 'a,b,c', 'var2': 1},
                             {'var1': 'd,e,f', 'var2': 2}])
              b = DataFrame([{'var1': 'a', 'var2': 1},
                             {'var1': 'b', 'var2': 1},
                             {'var1': 'c', 'var2': 1},
                             {'var1': 'd', 'var2': 2},
                             {'var1': 'e', 'var2': 2},
                             {'var1': 'f', 'var2': 2}])
              ### My solution
              import pandas as pd
              import functools
              def expand_on_cols(df, fuse_cols, delim=","):
                  def expand_on_col(df, fuse_col):
                      col_order = df.columns
                      df_expanded = pd.DataFrame(
                          df.set_index([x for x in df.columns if x != fuse_col])[fuse_col]
                          .apply(lambda x: x.split(delim))
                          .explode()
                      ).reset_index()
                      return df_expanded[col_order]
                  all_expanded = functools.reduce(expand_on_col, fuse_cols, df)
                  return all_expanded
              
              assert(b.equals(expand_on_cols(a, ["var1"], delim=",")))
              

              【讨论】:

                【解决方案19】:

                我想出了以下解决这个问题的方法:

                def iter_var1(d):
                    for _, row in d.iterrows():
                        for v in row["var1"].split(","):
                            yield (v, row["var2"])
                
                new_a = DataFrame.from_records([i for i in iter_var1(a)],
                        columns=["var1", "var2"])
                

                【讨论】:

                  【解决方案20】:

                  另一种使用python复制包的解决方案

                  import copy
                  new_observations = list()
                  def pandas_explode(df, column_to_explode):
                      new_observations = list()
                      for row in df.to_dict(orient='records'):
                          explode_values = row[column_to_explode]
                          del row[column_to_explode]
                          if type(explode_values) is list or type(explode_values) is tuple:
                              for explode_value in explode_values:
                                  new_observation = copy.deepcopy(row)
                                  new_observation[column_to_explode] = explode_value
                                  new_observations.append(new_observation) 
                          else:
                              new_observation = copy.deepcopy(row)
                              new_observation[column_to_explode] = explode_values
                              new_observations.append(new_observation) 
                      return_df = pd.DataFrame(new_observations)
                      return return_df
                  
                  df = pandas_explode(df, column_name)
                  

                  【讨论】:

                    【解决方案21】:

                    这里有很多答案,但我很惊讶没有人提到内置的 pandas 爆炸功能。查看以下链接: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.explode.html#pandas.DataFrame.explode

                    由于某种原因我无法访问该功能,所以我使用了以下代码:

                    import pandas_explode
                    pandas_explode.patch()
                    df_zlp_people_cnt3 = df_zlp_people_cnt2.explode('people')
                    

                    以上是我的数据样本。如您所见,people 列有一系列人员,我试图将其分解。我给出的代码适用于列表类型数据。因此,请尝试将逗号分隔的文本数据转换为列表格式。此外,由于我的代码使用内置函数,它比自定义/应用函数快得多。

                    注意:您可能需要使用 pip 安装 pandas_explode。

                    【讨论】:

                      【解决方案22】:

                      我遇到了类似的问题,我的解决方案是先将数据框转换为字典列表,然后再进行转换。这是函数:

                      import re
                      import pandas as pd
                      
                      def separate_row(df, column_name):
                          ls = []
                          for row_dict in df.to_dict('records'):
                              for word in re.split(',', row_dict[column_name]):
                                  row = row_dict.copy()
                                  row[column_name]=word
                                  ls.append(row)
                          return pd.DataFrame(ls)
                      

                      例子:

                      >>> from pandas import DataFrame
                      >>> import numpy as np
                      >>> a = DataFrame([{'var1': 'a,b,c', 'var2': 1},
                                     {'var1': 'd,e,f', 'var2': 2}])
                      >>> a
                          var1  var2
                      0  a,b,c     1
                      1  d,e,f     2
                      >>> separate_row(a, "var1")
                        var1  var2
                      0    a     1
                      1    b     1
                      2    c     1
                      3    d     2
                      4    e     2
                      5    f     2
                      

                      您还可以稍微更改函数以支持分隔列表类型的行。

                      【讨论】:

                        【解决方案23】:

                        在此页面上的所有解决方案中添加一些零碎的东西后,我能够得到类似的东西(对于需要立即使用它的人)。 该函数的参数是 df(输入数据帧)和 key(具有分隔符分隔字符串的列)。如果与分号“;”不同,只需替换为您的分隔符即可。

                        def split_df_rows_for_semicolon_separated_key(key, df):
                            df=df.set_index(df.columns.drop(key,1).tolist())[key].str.split(';', expand=True).stack().reset_index().rename(columns={0:key}).loc[:, df.columns]
                            df=df[df[key] != '']
                            return df
                        

                        【讨论】:

                          【解决方案24】:

                          使用assignexplode 的单线:

                              col1  col2
                          0  a,b,c     1
                          1  d,e,f     2
                          
                          df.assign(col1 = df.col1.str.split(',')).explode('col1', ignore_index=True)
                          

                          输出:

                            col1  col2
                          0    a     1
                          1    b     1
                          2    c     1
                          3    d     2
                          4    e     2
                          5    f     2
                          

                          【讨论】:

                            【解决方案25】:

                            试试:

                            vals = np.array(a.var1.str.split(",").values.tolist())    
                            var = np.repeat(a.var2, vals.shape[1])
                            
                            out = pd.DataFrame(np.column_stack((var, vals.ravel())), columns=a.columns)
                            display(out)
                            
                                  var1 var2
                                0   1   a
                                1   1   b
                                2   1   c
                                3   2   d
                                4   2   e
                                5   2   f
                            
                            

                            【讨论】:

                              【解决方案26】:

                              在最新版本的 pandas 中,您可以使用 split 后跟 explode

                              a.assign(var1=a['var1'].str.split(',')).explode('var1')
                              

                              一个

                                 var1 var2
                              0   a   1
                              0   b   1
                              0   c   1
                              1   d   2
                              1   e   2
                              1   f   2
                              

                              【讨论】:

                                猜你喜欢
                                • 2015-06-25
                                • 2016-09-16
                                • 2019-05-17
                                • 2019-02-12
                                • 2021-02-02
                                • 2017-05-08
                                • 1970-01-01
                                相关资源
                                最近更新 更多