Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I want to join the entries of the col1 with col2 along with a '.'(dot) in between. 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). and which suits in my case.

like image 398
tarun sahu Avatar asked Jun 10 '18 10:06

tarun sahu


People also ask

What is the difference between map (str) and astype (STR)?

what is the difference between .map (str) and .astype (str). and which suits in my case. Show activity on this post. map will take every single element of the original list and apply a function or lambda expression. In this compact form, your function is str (). It has more applications than that.

What is astype in pandas Dataframe?

DataFrame.astype() method is used to cast a pandas object to a specified dtype. astype() function also provides the capability to convert any suitable existing column to categorical type. DataFrame.astype() function comes very handy when we want to case a particular column data type to another data type.

What is the function str () in a Dataframe?

In this compact form, your function is 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.

How to cast a pandas Dataframe to another data type?

DataFrame.astype () method is used to cast a pandas object to a specified dtype. astype () function also provides the capability to convert any suitable existing column to categorical type. DataFrame.astype () function comes very handy when we want to case a particular column data type to another data type.


1 Answers

map will take every single element of the original list and apply a function or lambda expression. In this compact form, your function is 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.

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. Time-it to verify. To be noted: the astype cast, as also with map, creates a new object, not mutating the existent.

like image 143
Attersson Avatar answered Nov 15 '22 00:11

Attersson