这一行代码运行得很完美,但我对它的实际功能并不是很了解:
df['New_Column1'], shrink['New_Column2'] = zip(*df['Original_Column'].apply(lambda x: x.split('-', 1)))
这基本上是将一列拆分为一个破折号,该破折号将员工ID号和他们的实际姓名分隔开(在原始数据中,他们合并在一列中)。你知道吗
这是最有效的方法吗?我该如何向别人描述这件事?我以前从未使用过“zip”函数,我也不是100%使用lambda,但有时我会使用它来更改日期格式。你知道吗
是的,你是对的,这基本上是将一列拆分为一个破折号,将员工ID号和他们的实际姓名分开(在原始数据中,他们合并在一列中)。
我将给您介绍一下
zip
和lambda
的工作:^{} from the docs:
示例:
^{} from the docs:
示例:
apply
方法为序列的每一行调用lambda函数一次,df['Original Column']
。为每行调用一次Python函数是一个方法 对于慢度(如果序列有许多行)。一般来说,要最大限度地提高性能 使用Pandas时,只有在没有其他选项的情况下才使用apply
方法。你知道吗在这里使用
zip
会增加效率。zip
返回Python列表 元组数。Python列表和元组比Pandas系列需要更多的空间 当序列中的值是本机NumPy数据类型时。字符串可以是 由本机NumPy数据类型表示,因此将数据保留在一个系列中更为重要 节省空间。所以zip
,像apply
,如果可能的话,这里应该避免。你知道吗在本例中,您可以代替Panda的vectorized string method, ^{} :
收益率
extract
的参数是regex模式。r'([^-]*)-?(.*)'
具有以下含义:用括号括起来的模式定义组,然后由
extract
方法。你知道吗相关问题 更多 >
编程相关推荐