删除pandas DataFrame列中的字符串条目的结尾 [英] Remove ends of string entries in pandas DataFrame column
本文介绍了删除pandas DataFrame列中的字符串条目的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个熊猫数据框,其中有一列是文件列表
I have a pandas Dataframe with one column a list of files
import pandas as pd
df = pd.read_csv('fname.csv')
df.head()
filename A B C
fn1.txt 2 4 5
fn2.txt 1 2 1
fn3.txt ....
....
我想从文件
中的每个条目中删除文件扩展名 .txt
。我该怎么做?
I would like to delete the file extension .txt
from each entry in filename
. How do I accomplish this?
我尝试过:
df['filename'] = df['filename'].map(lambda x: str(x)[:-4])
但是当我之后用 df.head()
查看列条目时,没有任何变化。
but when I look at the column entries afterwards with df.head()
, nothing has changed.
这是怎么做的?
推荐答案
我认为您可以使用 str.replace
和正则表达式 .txt $ '
( $
-匹配字符串的结尾):
I think you can use str.replace
with regex .txt$'
( $
- matches the end of the string):
import pandas as pd
df = pd.DataFrame({'A': {0: 2, 1: 1},
'C': {0: 5, 1: 1},
'B': {0: 4, 1: 2},
'filename': {0: "txt.txt", 1: "x.txt"}},
columns=['filename','A','B', 'C'])
print df
filename A B C
0 txt.txt 2 4 5
1 x.txt 1 2 1
df['filename'] = df['filename'].str.replace(r'.txt$', '')
print df
filename A B C
0 txt 2 4 5
1 x 1 2 1
df['filename'] = df['filename'].map(lambda x: str(x)[:-4])
print df
filename A B C
0 txt 2 4 5
1 x 1 2 1
df['filename'] = df['filename'].str[:-4]
print df
filename A B C
0 txt 2 4 5
1 x 1 2 1
编辑:
rstrip
可以删除更多字符,如果字符串的末尾包含带条纹的字符串的某些字符(在本例中为。
, t
, x
):
rstrip
can remove more characters, if the end of strings contains some characters of striped string (in this case .
, t
, x
):
示例:
print df
filename A B C
0 txt.txt 2 4 5
1 x.txt 1 2 1
df['filename'] = df['filename'].str.rstrip('.txt')
print df
filename A B C
0 2 4 5
1 1 2 1
这篇关于删除pandas DataFrame列中的字符串条目的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文