Python 将列中的字符串部分替换为`and`条件
我有一个Python 将列中的字符串部分替换为`and`条件,python,string,pandas,dataframe,replace,Python,String,Pandas,Dataframe,Replace,我有一个熊猫数据框,看起来像这样: Size Measure Location Messages Small 1 Washington TXT0123 TXT0875 TXT874 TXT0867 TXT0875 TXT0874 Medium 2 California TXT020 TXT017 TXT120 TXT012 Large 3 Texas TXT0
熊猫数据框
,看起来像这样:
Size Measure Location Messages
Small 1 Washington TXT0123 TXT0875 TXT874 TXT0867 TXT0875 TXT0874
Medium 2 California TXT020 TXT017 TXT120 TXT012
Large 3 Texas TXT0123 TXT0123 TXT0123 TXT0123 TXT0217 TXT0206
Small 4 California TXT020 TXT0217 TXT006
Tiny 5 Nevada TXT0206 TXT0217 TXT0206
如果长度等于7,第四个字符为0,我试图从Messages
列中的单个单词中删除0
我尝试了for循环,但它正在删除所有0:
for line in df.Messages:
for message in line.split():
if len(message) == 7 and message[3] == '0':
print(message.replace('0', ''))
我还尝试了.map
,这给了我一些错误:
df.Messages = df.Messages.map(lambda x: x.replace('0', '') for message in line.split() for line in df.Messages if (len(message) == 7 and message[3] == '0'))
TypeError: 'generator' object is not callable
有没有一种方法可以使用
.map
来实现这一点,它包括if
和以及条件?如果您想对每个单词执行此操作,请首先使用str.split
拆分列,调用apply
,然后使用str.join
重新合并:
def f(l):
return [w.replace('0', '') if len(w) == 7 and w[3] == '0' else w for w in l]
df.Messages.str.split().apply(f).str.join(' ')
0 TXT123 TXT875 TXT874 TXT867 TXT875 TXT874
1 TXT020 TXT017 TXT120 TXT012
2 TXT123 TXT123 TXT123 TXT123 TXT217 TXT26
3 TXT020 TXT217 TXT006
4 TXT26 TXT217 TXT26
Name: Messages, dtype: object
如果只想替换单个0(而不是全部),请在函数f
中使用w.replace('0','',1)
。如果要对每个单词执行此操作,请首先使用str.split
拆分列,调用apply
,然后使用str.join
:
def f(l):
return [w.replace('0', '') if len(w) == 7 and w[3] == '0' else w for w in l]
df.Messages.str.split().apply(f).str.join(' ')
0 TXT123 TXT875 TXT874 TXT867 TXT875 TXT874
1 TXT020 TXT017 TXT120 TXT012
2 TXT123 TXT123 TXT123 TXT123 TXT217 TXT26
3 TXT020 TXT217 TXT006
4 TXT26 TXT217 TXT26
Name: Messages, dtype: object
df.Messages.str.split().apply(pd.Series).fillna('').\
applymap(lambda x : x[:2]+x[4:] if len(x)==7 and x[3]=='0' else x).\
apply(' '.join,1)
如果只想替换单个0(而不是全部),请使用函数f
中的w.replace('0','',1)
df.Messages.str.split().apply(pd.Series).fillna('').\
applymap(lambda x : x[:2]+x[4:] if len(x)==7 and x[3]=='0' else x).\
apply(' '.join,1)
出[597]:
0 TX123 TX875 TXT874 TX867 TX875 TX874
1 TXT020 TXT017 TXT120 TXT012
2 TX123 TX123 TX123 TX123 TX217 TX206
3 TXT020 TX217 TXT006
4 TX206 TX217 TX206
dtype: object
出[597]:
0 TX123 TX875 TXT874 TX867 TX875 TX874
1 TXT020 TXT017 TXT120 TXT012
2 TX123 TX123 TX123 TX123 TX217 TX206
3 TXT020 TX217 TXT006
4 TX206 TX217 TX206
dtype: object
IIUC:
IIUC:
您希望将此条件应用于每个单词吗?还是整个字符串?@cᴏʟᴅsᴘᴇᴇᴅ 对不起,每个单词。更新的问题。是否要将此条件应用于每个单词?还是整个字符串?@cᴏʟᴅsᴘᴇᴇᴅ 对不起,每个单词。更新的问题。TXT0206还是TX206吗?@Wen-Hmm。。。好问题。。。我不确定。您只需使用str.replace(…,1)
即可。@cᴏʟᴅsᴘᴇᴇᴅ 谢谢你!我只想要一个0,虽然直到提到TXT0206我才意识到。。。谢谢你的帮助。TXT0206还是TX206吗?@Wen-Hmm。。。好问题。。。我不确定。您只需使用str.replace(…,1)
即可。@cᴏʟᴅsᴘᴇᴇᴅ 谢谢你!我只想要一个0,虽然直到提到TXT0206我才意识到。。。谢谢你的帮助。