Concatenate results of `apply` in pandas.

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

Concatenate results of `apply` in pandas

问题

我想对pandas Series/DataFrame的每个元素/行应用一个函数,并将结果连接/堆叠成一个单独的DataFrame。
例如,我可以从一个Series开始,s = pd.Series(["a", "b,c", "d,e,f"]), 然后我想得到最终的结果 res = pd.Series(["a", "b", "c", "d", "e", "f"])

一个较慢的方法是:

res = []
for _, x in s.items():
    res.append(pd.Series(x.split(",")))
res = pd.concat(res, ignore_index=True)

我想探索pandas的内部功能。似乎可以通过类似于 s.apply(lambda x: x.split(","))s.str.split() 的方式来实现这一点,它会给出一个包含列表的Series...

备注:

  • 上面的简单例子实际上可以通过类似于 pd.Series(",".join(s.tolist()).split(",")) 的方式解决,但我正在寻找一个通用的解决方案。

  • 请注意,这不是 Apply elementwise, concatenate resulting rows into a DataFrame 的重复(在该问题中,“apply”是以一种通用的方式使用,而不是作为pandas函数的名称)。

英文:

I would like to apply a function to each element/row of a pandas Series/DataFrame and concatenate/stack the results into a single DataFrame.
E.g., I may start with a Series s = pd.Series(["a", "b,c", "d,e,f"]), and I would like to obtain as a final result res = pd.Series(["a", "b", "c", "d", "e", "f"])

A slow way of doing this would be:

res = []
for _, x in s.items():
    res.append(pd.Series(s.split(",")))
res = pd.concat(res, ignore_index=True)

I would like to explore the internal functionality of pandas. It seems that there should be a way of doing this by starting with something like s.apply(lambda x: x.split(",")) or s.str.split(), which gives a series of lists...

Remarks:

  • The simple example above could be actually solved using something like pd.Series(",".join(s.tolist()).split(",")), but I am looking for a generalizable solution.

  • Note that this is not a duplicate of Apply elementwise, concatenate resulting rows into a DataFrame (where apply is used in a generic sense rather than as a name of pandas function.)

答案1

得分: 2

可以在两个步骤中完成此操作:

  • 将每个字符串拆分为由逗号分隔的字符串列表
  • 将由字符串列表组成的列展开为一系列字符串

这可以在一行中完成,结合使用map和flatmap:

s.str.split(',').explode()
英文:

You can achieve this in two steps:

  • split each string into a list of strings delimiting by comma
  • explode the column made up of lists of strings into a series of strings

This can be achieved in a single line that combines the map with the flatmap:

s.str.split(',').explode()

huangapple
  • 本文由 发表于 2023年3月31日 18:13:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75897342.html
匿名

发表评论

匿名网友

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

确定