繁体   English   中英

如果行包含 pandas 中的特定字符串,如何跳过拆分?

[英]How to skip splitying if row contains specific string in pandas?

我有一列包含 state 和国家名称:

Name   Region       Value_1 etc.
Apple  Penn State    5641561
Apple  Boston State   21515151
Apple  United States  5545645
etc.

我想在空格(“”)之后删除字符串,但我想保持美国原样。

例如:

Name   Region       Value_1 etc.
Apple  Penn          5641561
Apple  Boston         21515151
Apple  United States  5545645
etc.

我怎样才能做到这一点? 我正在使用以下代码进行拆分: df['Region'] = df['Region'].str.split(' ').str[0]

IIUC,您可以使用Series.str.replace将系列中出现的模式替换为替换字符串:

df['Region'] = df['Region'].str.replace(r'(\sState)\b', '')

结果:

# print(df)

    Name         Region   Value_1
0  Apple           Penn   5641561
1  Apple         Boston  21515151
2  Apple  United States   5545645

尝试这个:

df = pd.DataFrame({'Name': ['Apple', 'Apple', 'Apple'], 'Region': ['Penn State', 'Boston State', 'United States']})
df['Region'] = df['Region'].apply(lambda x: x.replace('State', '') if x.split()[-1].strip() == 'State' else x)

Output:


    Name    Region
0   Apple   Penn
1   Apple   Boston
2   Apple   United States

替代使用np.where()

### Create DataFrame
df = pd.DataFrame({
'Name': ['Apple', 'Apple', 'Apple'],
'Region': ['Penn State', 'Boston State', 'United States'],
'Value_1': [5641561, 21515151, 554564]
})

### Using np.where()
df['Region'] = df['Region'].where(df['Region'].str.contains('United States'), 
                            df['Region'].str.split(" ").str[0])

### Output
print(df)

    Name         Region   Value_1
0  Apple           Penn   5641561
1  Apple         Boston  21515151
2  Apple  United States    554564

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM