如果我有这样的桌子
|---------------------|------------------|
| time | list of string |
|---------------------|------------------|
| 2019-06-18 09:05:00 | ['A','B','C']|
|---------------------|------------------|
| 2019-06-19 09:05:00 | ['A','C'] |
|---------------------|------------------|
| 2019-06-19 09:05:00 | ['B','C'] |
|---------------------|------------------|
| 2019-06-20 09:05:00 | ['C'] |
|---------------------|------------------|
| 2019-06-20 09:05:00 | ['A','C']|
|---------------------|------------------|
对于每一行,我想知道当前时间戳之前的几行与当前字符串列表至少有一个公共值。
慢速代码如下:
results = [] for i in range(len(df)):
current_t = df['time'].iloc[i]
current_string = df['list_of_string'].iloc[i]
df_before_t = df[df['time']<current_t]
cumm_count = 0
for row in df_before_t['list_of_string']:
if (set(current_string) & set(row)):
cumm_count += 1
results.append(cumm_count)
所以结果表将是:
|---------------------|------------------|---------------------|
| time | list of string | result |
|---------------------|------------------|---------------------|
| 2019-06-18 09:05:00 | ['A','C']| 0 |
|---------------------|------------------|---------------------|
| 2019-06-19 09:05:00 | ['A','C'] | 1 |
|---------------------|------------------|---------------------|
| 2019-06-19 09:05:00 | ['D'] | 0 |
|---------------------|------------------|---------------------|
| 2019-06-20 09:05:00 | ['C'] | 2 |
|---------------------|------------------|---------------------|
| 2019-06-20 09:05:00 | ['A','C']| 2 |
|---------------------|------------------|---------------------|
我当前拥有的数据集相对较大,我希望获得帮助以快速处理此数据。非常感谢!