拆分 pandas 行中的字符串并通过放大数据框插入新行

Tan*_*zer 5 python dataframe pandas

我有以下数据框:

单词 地位
0 0 去检查
1 1 去检查
2 2 :) 表情符号
3 3 博士。 去检查
4 4 “未来” 去检查
5 5 去检查
6 6 去检查

我想遍历每一行以查找单词初始和最终位置处的引号,并创建一个如下所示的 DataFrame:

单词 地位
0 0 去检查
1 1 去检查
2 2 :) 表情符号
3 3 博士。 去检查
4 4 引号
5 4 未来 单词
6 4 引号
7 5 去检查
8 6 去检查

我可以去掉引号并将单词分成三部分,但我得到了这个 DataFrame,它覆盖了最后两行:

单词 地位
0 0 去检查
1 1 去检查
2 2 :) 表情符号
3 3 博士。 去检查
4 4 引号
5 4 未来 单词
6 4 引号

我尝试了 df.loc[index]、df.iloc[index]、df.at[index] 但它们都没有帮助我扩展 DataFrame 中的行数。

是否可以在特定索引处添加新行而不覆盖最后两行?

WeN*_*Ben 6

在你的情况下,你split可以explode

out = df.assign(word = df.word.str.split(r'(\")')).explode('word').\
           loc[lambda x : x['word']!='']
   no    word    status
0   0     one  to_check
1   1     two  to_check
2   2      :)  emoticon
3   3     dr.  to_check
4   4       "  to_check
4   4  future  to_check
4   4       "  to_check
5   5      to  to_check
6   6      be  to_check
Run Code Online (Sandbox Code Playgroud)

用于改变状态

out['status'] = np.where(out['word'].eq('"'), 'quotes',out['status'])
Run Code Online (Sandbox Code Playgroud)