pandas 字符串切片后保存_Pandas series.str.get 获取字符列切片

本文介绍了Python中的Pandas库,特别是Pandas系列的str.get()方法,用于按位置提取字符串元素。该方法适用于字符串、数值甚至列表。通过示例展示了如何从字符串列中获取单个字符,以及如何从包含列表的列中提取列表中的元素。
摘要由CSDN通过智能技术生成

Python | Pandas series.str.get()

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.get() method is used to get element** at the passed position.** This method works for string, numeric values and even lists throughout the series. **.str **has to be prefixed every time to differentiate it from Python’s default get() method.

**Syntax: **Series.str.get(i)

**Parameters: **

**i : **Position of element to be extracted, Integer values only.

**Return type: **Series with element/character at passed position

To download the CSV used in code, click here.

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

Example 1: Getting character from string value

In this example, str.get() method is used to get a single character from the Name column. The *null *values have been removed using dropna() method and the series is converted to string type series using .astype() before applying this method. This method can be used to get one character instead of whole string. For example, getting M from Male and F from Female since there can be two inputs only, so doing this can save data.

# importing pandas module

import pandas as pd

# reading csv file from url

data = pd.read_csv("https://cdncontribute.geeksforgeeks.org/wp-content/uploads/nba.csv")

# dropping null value columns to avoid errors

data.dropna(inplace = True)

# converting to string series

data["Name"]= data["Name"].astype(str)

# creating new column with element at 0th position in data["Team"]

data["New"]= data["Name"].str.get(0)

data

# display

Output:

As shown in the output image, the New column is having first letter of the string in Name column.

Example #2: Getting elements from series of List

In this example, the Team column has been split at every occurrence of ” ” (Whitespace), into a list using *str.split() *method. Then the same column is overwritten with it. After that *str.get() *method is used to get elements in list at passed index.

# importing pandas module

import pandas as pd

# reading csv file from url

data = pd.read_csv("https://cdncontribute.geeksforgeeks.org/wp-content/uploads/nba.csv")

# dropping null value columns to avoid errors

data.dropna(inplace = True)

# converting to string series

data["Team"]= data["Team"].astype(str)

# splitting at occurence of whitespace

data["Team"]= data["Team"].str.split(" ", 1)

# displaying first element from list

data["Team"].str.get(0)

# displaying second element from list

data["Team"].str.get(1)

Output:

As shown in the output images, The first image is of Elements at 0th position in series and the second image is of elements at 1st position in the series.

Output 1:data["Team"].str.get(0)

Output 2: data["Team"].str.get(1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值