【数据平台】dataframe一列成多列

1、场景:一列中的值出现 1|2|3|56,这样用|分割的值,需要将其转换成4列。


2、操作:

names=df['names'].str.split('|',expand=True)#多名字分列
names.columns=['ids0','ids1','ids2','ids3','ids4','ids5','ids6','ids7']#
df=df.join(names)
 
  

其中names字段的列是包含|分割的值,最后将多列合并会原dataframe里。


3、函数:

Series.str. split ( pat=None, n=-1, expand=False ) [source]

Split each string (a la re.split) in the Series/Index by givenpattern, propagating NA values. Equivalent to str.split().

Parameters:

pat : string, default None

String or regular expression to split on. If None, splits on whitespace

n : int, default -1 (all)

None, 0 and -1 will be interpreted as return all splits

expand : bool, default False

  • If True, return DataFrame/MultiIndex expanding dimensionality.
  • If False, return Series/Index.

return_type : deprecated, use expand

Returns:

split : Series/Index or DataFrame/MultiIndex of objects


 
  
   
   

你可能感兴趣的:(Big,data,python专栏)