<p><strong>我将使用<a href="https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html" rel="nofollow noreferrer">Google Doc String Format</a>并转换您的代码。重要的是给函数和参数命名为易懂的名称,以便它们易于理解和解释</strong></p>
<hr/>
<pre class="lang-py prettyprint-override"><code>def split_columns(brands, brands_data):
"""Used to split column string on the `|` character
Args:
brands (str): String containing brand names separated
by `|` character to be seperated
brands_data (pandas df column): Dataframe column containing the information
Returns:
The updated dataframe column
"""
for brand in brands:
brands_data[brand]=brands_data[brand].apply(lambda x: x.split('|'))
return brands_data
</code></pre>
<hr/>
<p><strong>这么说,我觉得这里有些不对劲。</strong></p>
<p>如果您只是简单地传入一列,而您所要做的只是将该列返回,这样每个条目现在都是一个嵌套列表<br/><br/>
<strong><code>'1|Versace|Levis|Adidas|' > ['1','Versace','Levis','Adidas']</code></strong></p>
<p>最简单的方法是不使用函数,直接调用<strong><code>.apply()</code></strong>:</p>
<hr/>
<p><em>假设一个名为<strong><code>df</code></strong>的数据帧和一个名为<strong><code>Brands</code></strong></em></p>
<pre class="lang-py prettyprint-override"><code>>>>df
Brands
0 0|Gucci|Prada|
1 1|Versace|Levis|Adidas|
2 2|Champion|Diesel|Nike|
>>>df["Brands"] = df["Brands"].apply(lambda row: row.split('|'))
>>>df
Brands
0 [0, Gucci, Prada, ]
1 [1, Versace, Levis, Adidas, ]
2 [2, Champion, Diesel, Nike, ]
</code></pre>
<p><strong>希望这一澄清能对您有所帮助</strong></p>