简体   繁体   English

数据帧中.map(str)和.astype(str)之间的区别是什么

[英]what is difference between .map(str) and .astype(str) in dataframe

I have a dataframe with column name col1 and col2 of integer type entries. 我有一个数据框,列名为col1,整数类型为col2。 I want to join the entries of the col1 with col2 along with a '.'(dot) in between. 我想加入col1的条目与col2以及中间的'。'(点)。 I have searched and found to add two column entries : 我搜索并发现添加两个列条目:

df['col'] = df['col1'].map(str) + df['col2'].map(str)

and for add a dot : 并添加一个点:

df['col'] = df['col1'].astype(str) + '.'

but I want something like this 但是我想要这样的东西

df['col'] = each entries of df['col1'] + '.' + each entries of df['col2']

what is the difference between .map(str) and .astype(str). .map(str)和.astype(str)之间的区别是什么。 and which suits in my case. 哪个适合我的情况。

map will take every single element of the original list and apply a function or lambda expression. map将获取原始列表的每个元素并应用函数或lambda表达式。 In this compact form, your function is str() . 在这个紧凑的形式中,您的函数是str() It has more applications than that. 它有更多的应用程序。 You could for example edit every element returning a new list. 例如,您可以编辑返回新列表的每个元素。 This is possible because a DataFrame cell is castable to string. 这是可能的,因为DataFrame单元可以转换为字符串。

astype is a Pandas function for DataFrames (and numpy for numpy arrays) that will cast the object to the specified type and therefore here it makes little practical difference except it may be more performant since it is just 1 operation compared to multiple calls and it is natively defined in Pandas. astypeastype的Pandas函数(和Numpy数组的numpy),它会将对象强制转换为指定的类型,因此它在实际上没什么区别,除了它可能更高效,因为与多次调用相比它只是1次操作,它是本地在熊猫中定义。 Time-it to verify. 时间 - 验证。 To be noted: the astype cast, as also with map , creates a new object, not mutating the existent. 需要注意的是: astype以及map也会创建一个新对象,而不会改变现有对象。

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

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