擅长:python、mysql、java
<p>熊猫0.23有一个处理文本的好方法。参见文档<a href="https://pandas.pydata.org/pandas-docs/stable/text.html" rel="nofollow noreferrer">Working with Text Data</a>。可以使用<a href="https://docs.python.org/3/library/re.html" rel="nofollow noreferrer">regular expressions</a>捕获和替换文本</p>
<pre><code>import pandas as pd
df = pd.DataFrame({'CityLocation': ["Bangalore / Palo Alto", "Bangalore / SFO", "Other"]})
df['CityLocation'] = df['CityLocation'].str.replace("^Bang.*", "Bangalore")
print(df)
</code></pre>
<p>将屈服</p>
<pre><code> CityLocation
0 Bangalore
1 Bangalore
2 Other
</code></pre>