Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我有熊猫阵列,它最初是一个csv文件。我想从列中的所有行中删除一个特定单词:text_Python_Pandas - Fatal编程技术网

Python 我有熊猫阵列,它最初是一个csv文件。我想从列中的所有行中删除一个特定单词:text

Python 我有熊猫阵列,它最初是一个csv文件。我想从列中的所有行中删除一个特定单词:text,python,pandas,Python,Pandas,这是熊猫阵列: id text spam 4016 Subject: re : vacation vince : i just found ... 0 4017 Subject: re : receipts from visit jim , than... 0 4018 Subject: re : enron case study update wow ! a.

这是熊猫阵列:

id          text                                       spam
4016        Subject: re : vacation vince : i just found ... 0
4017        Subject: re : receipts from visit jim , than... 0
4018        Subject: re : enron case study update wow ! a...0
4019        Subject: re : interest david , please , call... 0
4020        Subject: news : aurora 5 . 2 update aurora ve...0
我想从所有行中删除“文本”列中的“主题”一词,使其成为:

id          text                                       spam
4016        re : vacation vince : i just found ...  0
4017        re : receipts from visit jim , than...  0
4018        re : enron case study update wow ! a...0
4019        re : interest david , please , call...  0
4020        news : aurora 5 . 2 update aurora ve...0
试试这个:

df.text = df.text.apply(lambda row: row[9:])
每一行都将在“文本”列中更改,其中前9个字符“主题:”将被删除。

尝试以下操作:

df.text = df.text.apply(lambda row: row[9:])
每一行将在“文本”列更改,其中前9个字符“主题:”将被删除。

我认为您需要-
^
表示每个字符串的开头和
\s+
一个或多个空格:

df['text'] = df['text'].replace('^Subject:\s+', '', regex=True)
print (df)
     id                                     text  spam
0  4016   re : vacation vince : i just found ...     0
1  4017   re : receipts from visit jim , than...     0
2  4018  re : enron case study update wow ! a...     0
3  4019   re : interest david , please , call...     0
4  4020  news : aurora 5 . 2 update aurora ve...     0
但如果需要,请先删除
9
字符,包括
空格
s:

df['text'] = df['text'].str[9:]
我认为您需要-
^
表示每个字符串的开头和
\s+
一个或多个空格:

df['text'] = df['text'].replace('^Subject:\s+', '', regex=True)
print (df)
     id                                     text  spam
0  4016   re : vacation vince : i just found ...     0
1  4017   re : receipts from visit jim , than...     0
2  4018  re : enron case study update wow ! a...     0
3  4019   re : interest david , please , call...     0
4  4020  news : aurora 5 . 2 update aurora ve...     0
但如果需要,请先删除
9
字符,包括
空格
s:

df['text'] = df['text'].str[9:]