我有一个.xlsx文件,我打开这段代码:
import pandas as pd
df = pd.read_excel(open('file.xlsx','rb'))
df['Description'].head
我有以下结果,看起来很不错。
ID | Description
:----- | :-----------------------------
0 | Some Description with no hash
1 | Text with #one hash
2 | Text with #two #hashes
现在我想创建一个新列,只保留以#开头的单词,如下所示:
ID | Description | Only_Hash
:----- | :----------------------------- | :-----------------
0 | Some Description with no hash | Nan
1 | Text with #one hash | #one
2 | Text with #two #hashes | #two #hashes
我能够使用#:
计算/分隔行descriptionWithHash = df['Description'].str.contains('#').sum()
但现在我想像上面描述的那样创建列。最简单的方法是什么?
问候!
PS:它应该在问题中显示表格格式,但我无法弄清楚它出现错误的原因!
答案 0 :(得分:5)
您可以str.findall
使用str.join
:
df['new'] = df['Description'].str.findall('(\#\w+)').str.join(' ')
print(df)
ID Description new
0 0 Some Description with no hash
1 1 Text with #one hash #one
2 2 Text with #two #hashes #two #hashes
对于NaNs:
df['new'] = df['Description'].str.findall('(\#\w+)').str.join(' ').replace('',np.nan)
print(df)
ID Description new
0 0 Some Description with no hash NaN
1 1 Text with #one hash #one
2 2 Text with #two #hashes #two #hashes
答案 1 :(得分:5)
In [126]: df.join(df.Description
...: .str.extractall(r'(\#\w+)')
...: .unstack(-1)
...: .T.apply(lambda x: x.str.cat(sep=' ')).T
...: .to_frame(name='Hash'))
Out[126]:
ID Description Hash
0 0 Some Description with no hash NaN
1 1 Text with #one hash #one
2 2 Text with #two #hashes #two #hashes