<p>将<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html" rel="nofollow noreferrer">^{<cd1>}</a>与由<code>dataframe2</code>创建的序列循环一起使用,<code>flags=re.I</code>参数用于非大小写匹配:</p>
<pre><code>import re
for k, v in dataframe2.set_index('ITEM/CODE')['TYPE'].items():
#if necessary word boundaries
pat = r"\b{}\b".format(k)
#if not
#pat = k
dataframe1.loc[dataframe1['DESC'].str.contains(pat, flags=re.I), 'CLASSIFICATION'] = v
print (dataframe1)
PN DESC CLASSIFICATION
0 C23890 Resistor 2.21K elec ELECTRONIC
1 C23891 Powerswitch NaN
2 C23892 Resistor 7.5K ELECTRONIC
3 C23893 Resistor .1K ELECTRONIC
4 C23894 FET elec NaN
5 C23895 ELE SD Card adapter ELECTRONIC
6 C23896 Crystal 16Mhz NaN
7 C23897 Capacitor 100uF ELECTRONIC
8 C23898 ELECTRONICS Resistor 10K ELECTRONIC
9 C23899 M3x5 Socket Cap Bolt MECH MECHANICAL
10 C23900 M3x6 Socket Cap Bolt Mech MECHANICAL
11 C23901 Mehcanical Assemble Kapton Tape 120mm NaN
12 C23902 MK7 Filament Drive Block Front NaN
13 C23903 Pulley 5mm shaft NaN
</code></pre>
<p>如果只想匹配第一个单词,请使用<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html" rel="nofollow noreferrer">^{<cd4>}</a>,但首先将这两个值转换为小写<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.lower.html" rel="nofollow noreferrer">^{<cd5>}</a>:</p>
<pre><code>dataframe2['ITEM/CODE'] = dataframe2['ITEM/CODE'].str.lower()
s = dataframe2.set_index('ITEM/CODE')['TYPE']
dataframe1['CLASSIFICATION'] = dataframe1['DESC'].str.split().str[0].str.lower().map(s)
print (dataframe1)
PN DESC CLASSIFICATION
0 C23890 Resistor 2.21K elec ELECTRONIC
1 C23891 Powerswitch NaN
2 C23892 Resistor 7.5K ELECTRONIC
3 C23893 Resistor .1K ELECTRONIC
4 C23894 FET elec NaN
5 C23895 ELE SD Card adapter ELECTRONIC
6 C23896 Crystal 16Mhz NaN
7 C23897 Capacitor 100uF ELECTRONIC
8 C23898 ELECTRONICS Resistor 10K ELECTRONIC
9 C23899 M3x5 Socket Cap Bolt MECH NaN
10 C23900 M3x6 Socket Cap Bolt Mech NaN
11 C23901 Mehcanical Assemble Kapton Tape 120mm NaN
12 C23902 MK7 Filament Drive Block Front NaN
13 C23903 Pulley 5mm shaft NaN
</code></pre>