[英]combine pandas rows based on condition of columns (vectorized)
我有这个数据框
import pandas as pd
df = pd.DataFrame({"a":[None, None, "hello1","hello2", None,"hello4","hello5","hello6", None, "hello8", None,"hello10",None ] , "b": ["we", "are the world", "we", "love", "the", "world", "so", "much", "and", "dance", "every", "day", "yeah"]})
a b
0 None we
1 None are the world
2 hello1 we
3 hello2 love
4 None the
5 hello4 world
6 hello5 so
7 hello6 much
8 None and
9 hello8 dance
10 None every
11 hello10 day
12 None yeah
所需的输出是:
a b new_text
0 Intro we we are the world
2 hello1 we we
3 hello2 love love the
5 hello4 world world
6 hello5 so so
7 hello6 much much and
9 hello8 dance dance every
11 hello10 day day yeah
我有一个函数可以做到这一点,但它在 Pandas 中使用,这可能不是最好的解决方案。
def connect_rows_on_condition(df, new_col_name, text, condition):
if df[condition][0] == None:
df[condition][0] = "Intro"
df[new_col_name] = ""
index = 1
last_non_none = 0
while index < len(df):
if df[condition][index] != None:
last_non_none = index
df[new_col_name][last_non_none] = df[text][index]
elif df[condition][index] == None :
df[new_col_name][last_non_none] = df[text][last_non_none] + " " + df[text][index]
index += 1
output_df = df[df[condition].isna() == False]
return output_df
主要逻辑是,如果“a”列中的“无”将 b 中的文本放入前一行。 有没有不基于循环的解决方案?
首先,创建一个描述组的系列:
grouping = df.a.notnull().cumsum()
然后,对于 a 列,我们可以使用第一个元素,对于 b 列,我们要连接所有元素:
df.groupby(grouping).agg({'a': 'first', 'b': ' '.join})
这给出:
a b
a
0 None we are the world
1 hello1 we
2 hello2 love the
3 hello4 world
4 hello5 so
5 hello6 much and
6 hello8 dance every
7 hello10 day yeah
如果需要,您可以将None
替换为"Intro"
作为特殊情况,因为该文本不会出现在输入中。
您还可以按单词进行分组,以防您没有空值可按此分组。 为了不重复约翰的解决方案,我将把它留在这里给那些可能对如何在非空情况下执行此操作感兴趣的人:
In [356]: Truth = pd.to_numeric(df.a.str.contains('None') == False).cumsum()
...:
In [357]: df.groupby(Truth)['b'].agg(list)
Out[357]:
a
0 [we, are the world]
1 [we]
2 [love, the]
3 [world]
4 [so]
5 [much, and]
6 [dance, every]
7 [day, yeah]
Name: b, dtype: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.