python - Why does pandas series.map method work for column concatenation? -


from couple of other posts, simple way concatenate columns in dataframe use map command, in example below. map function returns series, why can't regular series used instead of map?

import pandas pd df = pd.dataframe({'a':[1,2,3],'b':[4,5,6]},index=['m','n','o']) df['x'] = df.a.map(str) + "_x"        b   x  m   1   4   1_x n   2   5   2_x o   3   6   3_x 

this works though i'm creating series.

df['y'] = pd.series(df.a.map(str)) + "_y"        b   x    y m   1   4   1_x  1_y n   2   5   2_x  2_y o   3   6   3_x  3_y 

this doesn't work, gives typeeror

df['z'] = df['a'] + "_z" typeerror: unsupported operand type(s) +: 'numpy.ndarray' , 'str' 

this doesn't work either:

df['z'] = pd.series(df['a']) + "_z" typeerror: unsupported operand type(s) +: 'numpy.ndarray' , 'str' 

i checked see if map returns different type of object under hood, doesn't seem to:

type(pd.series(df.a.map(str))) pandas.core.series.series  type(pd.series(df['a'])) pandas.core.series.series 

i'm confused map doing makes work , how whatever map carries on subsequent string arithmetic.

map maps input values against corresponding value in passed in type.

normally passed in type series, dict or function, in case it's calling str ctor function , concatenating '_x'.

however, you've found out df['a'] + "_z" , pd.series(df['a']) + "_z" won't work there no operand defined types (ndarray str).

you using:

in [8]:     df['a'].astype(str) + '_z'  out[8]: m    1_z n    2_z o    3_z name: a, dtype: object 

the thing consider when call df['a'].map(str) dtype changed str:

in [13]:     df['a'].map(str).dtype ​ out[13]: dtype('o') 

so can see why first version worked changed dtype or series above same df['a'].astype(str)


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -