- API reference
- Series
- pandas.Serie...
pandas.Series.str.cat#
- Series.str.cat(others=None,sep=None,na_rep=None,join='left')[source]#
Concatenate strings in the Series/Index with given separator.
Ifothers is specified, this function concatenates the Series/Indexand elements ofothers element-wise.Ifothers is not passed, then all values in the Series/Index areconcatenated into a single string with a givensep.
- Parameters:
- othersSeries, Index, DataFrame, np.ndarray or list-like
Series, Index, DataFrame, np.ndarray (one- or two-dimensional) andother list-likes of strings must have the same length as thecalling Series/Index, with the exception of indexed objects (i.e.Series/Index/DataFrame) ifjoin is not None.
If others is a list-like that contains a combination of Series,Index or np.ndarray (1-dim), then all elements will be unpacked andmust satisfy the above criteria individually.
If others is None, the method returns the concatenation of allstrings in the calling Series/Index.
- sepstr, default ‘’
The separator between the different elements/columns. By defaultthe empty string‘’ is used.
- na_repstr or None, default None
Representation that is inserted for all missing values:
Ifna_rep is None, andothers is None, missing values in theSeries/Index are omitted from the result.
Ifna_rep is None, andothers is not None, a row containing amissing value in any of the columns (before concatenation) willhave a missing value in the result.
- join{‘left’, ‘right’, ‘outer’, ‘inner’}, default ‘left’
Determines the join-style between the calling Series/Index and anySeries/Index/DataFrame inothers (objects without an index needto match the length of the calling Series/Index). To disablealignment, use.values on any Series/Index/DataFrame inothers.
- Returns:
- str, Series or Index
Ifothers is None,str is returned, otherwise aSeries/Index(same type as caller) of objects is returned.
See also
Examples
When not passingothers, all values are concatenated into a singlestring:
>>>s=pd.Series(['a','b',np.nan,'d'])>>>s.str.cat(sep=' ')'a b d'
By default, NA values in the Series are ignored. Usingna_rep, theycan be given a representation:
>>>s.str.cat(sep=' ',na_rep='?')'a b ? d'
Ifothers is specified, corresponding values are concatenated withthe separator. Result will be a Series of strings.
>>>s.str.cat(['A','B','C','D'],sep=',')0 a,A1 b,B2 NaN3 d,Ddtype: object
Missing values will remain missing in the result, but can again berepresented usingna_rep
>>>s.str.cat(['A','B','C','D'],sep=',',na_rep='-')0 a,A1 b,B2 -,C3 d,Ddtype: object
Ifsep is not specified, the values are concatenated withoutseparation.
>>>s.str.cat(['A','B','C','D'],na_rep='-')0 aA1 bB2 -C3 dDdtype: object
Series with different indexes can be aligned before concatenation. Thejoin-keyword works as in other methods.
>>>t=pd.Series(['d','a','e','c'],index=[3,0,4,2])>>>s.str.cat(t,join='left',na_rep='-')0 aa1 b-2 -c3 dddtype: object>>>>>>s.str.cat(t,join='outer',na_rep='-')0 aa1 b-2 -c3 dd4 -edtype: object>>>>>>s.str.cat(t,join='inner',na_rep='-')0 aa2 -c3 dddtype: object>>>>>>s.str.cat(t,join='right',na_rep='-')3 dd0 aa4 -e2 -cdtype: object
For more examples, seehere.