将熊猫列重新组合到基于另一列的2D列表中

时间:2019-07-20 20:06:48

标签: python python-3.x pandas list

我有一个pandas数据集,其中有一列单词和一列整数(0,1)。出现在零(第一个整数或1之后)和1(包括1)之间的所有单词都应放入2D数组中。

让我解释一下:

考虑此熊猫数据框:

import pandas as pd

df = pd.DataFrame(columns=['Text','Selection_Values'])
df["Text"] = ["Hi", "this is", "just", "a", "single", "sentence.", "This", "is another one."]
df["Selection_Values"] = [0,0,0,0,0,1,0,1]
print(df)

这是示例数据集:

              Text  Selection_Values
0               Hi                 0
1          this is                 0
2             just                 0
3                a                 0
4           single                 0
5        sentence.                 1
6             This                 0
7  is another one.                 1

预期结果应该是:

[["Hi this is just a single sentence."],["This is another one"]]

您对此有任何想法吗?

这是我到目前为止所做的:

result = []

s = ""
for i in range(len(df["Text"])):
    s += df["Text"][i] + " "
    if df["Selection_Values"][i] == 1:
        result.append([s])
        s = ""

有效:

[['Hi this is just a single sentence. '], ['This is another one. ']]

...但这可能不是最佳方法。它根本没有使用熊猫框架。

3 个答案:

答案 0 :(得分:3)

使用shift + ' '.join。当然,这是假设每个句子都有一个结束1,并且没有悬挂的句子。


g = df['Selection_Values'].shift().eq(1).cumsum()

df['Text'].groupby(g).agg(' '.join).tolist()

['Hi this is just a single sentence.', 'This is another one.']

答案 1 :(得分:2)

这是一种可能的方法:

import pandas as pd

# Initialize example dataframe
df = pd.DataFrame(columns=['Text', 'Selection_Values'])
df["Text"] = ["Hi", "this is", "just", "a", "single", "sentence.", "This", "is another one."]
df["Selection_Values"] = [0, 0, 0, 0, 0, 1, 0, 1]

# Add column with an unique group ID for each sentence
df['group_id'] = df.Selection_Values.shift(1, fill_value=0).cumsum()

# Join the strings that have the same group ID
sentence_series = df.groupby('group_id')['Text'].agg(' '.join)

# Optional: convert result series to list
sentence_list = sentence_series.to_list()

print(sentence_list)
# Output:
# ['Hi this is just a single sentence.', 'This is another one.']

答案 2 :(得分:2)

使用numpy.split + Series.str.cat单线:

In [143]: [[s.str.cat(sep=' ')] for s in np.split(df.Text, df[df.Selection_Values == 1].index+1) if not s.empty]                               
Out[143]: [['Hi this is just a single sentence.'], ['This is another one.']]