【发布时间】:2019-11-29 09:02:31
【问题描述】:
我有一个 pandas 数据集,其中有一列单词和一列整数 (0,1)。所有出现在零(第一个整数,或在 1 之后)和 1(包括)之间的单词都应放入二维数组中。
让我解释一下:
考虑一下这个 pandas 数据框:
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. ']]
...但这可能不是最好的方法。它根本不使用 pandas 框架。
【问题讨论】:
标签: python python-3.x pandas list