zoukankan      html  css  js  c++  java
  • Pandas Series.str.cat()连接字符串

     

    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

    Pandas str.cat() is used to concatenate strings to the passed caller series of string. Distinct values from a different series can be passed but the length of both the series has to be same. .str has to be prefixed to differentiate it from the Python’s default method.

    Syntax: Series.str.cat(others=None, sep=None, na_rep=None)

    Parameters:
    others: Series, index, data frame or list of strings to concatenate
    sep: Separator to be put between the two strings
    na_rep: None or string value to replace in place of null values

    Return type: Series with concatenated string valus

    To download the Csv file used, click here.

    In the following examples, the data frame used contains data on some NBA players. The image of data frame before any operations is attached below.

     
    Example #1: Concatenating column with separator

    In this example, the Team column is concatenated at the end of Name column with separator “, “. The Name column is overwritten with the new series and the data frame is then displayed.

    filter_none

    brightness_4

    # importing pandas module
    import pandas as pd
      
    # importing csv from link
      
    # making copy of team column
    new = data["Team"].copy()
      
    # concatenating team with name column
    # overwriting name column
    data["Name"]= data["Name"].str.cat(new, sep =", ")
      
    # display
    data

    Output:
    As shown in the output image, every string in the Team column having same index as string in Name column have been concatenated with separator “, “.

     
    Example #2: Handling Null values

    The most important part in analyzing data is handling null values. str.cat() provides a way to handle null values through na_rep parameter. Whatever is passed to this parameter will be replaced at every occurrence of null value.
    In this example, college column is concatenated with team column. “No college” is passed to na_rep parameter to replace null with this string.

    filter_none

    brightness_4

    # importing pandas module
    import pandas as pd
      
    # importing csv from link
      
    # making copy of team column
    new = data["Team"].copy()
      
    # string to replace null values with
    na_string ="No College"
      
    # concatenating team with name column
    # overwriting name column
    data["College"]= data["College"].str.cat(new, sep =", ", na_rep = na_string)
      
    # display
    data

    Output:
    As it can be seen in the data frame, at index position 4 and 5, there was NULL value which has been replaced with “No College” and the string from Team column have been concatenated successfully.

  • 相关阅读:
    通过串口抓取图片
    Qt也有垃圾回收(通过QScopedPointer实现),下决心在项目里使用QScopedPointer,省了太多事情了,而且更安全!!
    IOS端 margin-top 和 margin-bottom 使用负数时的区别
    使用ROME解析rss,如何获取icon图标
    SVG图片如何调整大小和颜色
    Js点击触发Css3的动画Animations、过渡Transitions效果
    如何判断是否为同一个App,Ionic3如何修改包名
    如何使用JPQL写纯SQL语句
    为何在新线程中使用注解获取不到Spring管理的Bean
    Ionic的NavController 和ModalController 的区别
  • 原文地址:https://www.cnblogs.com/a00ium/p/13874633.html
Copyright © 2011-2022 走看看