Pandas 将元组中的字符串拆分成列
在本文中,我们将介绍如何使用Pandas将元组中的字符串拆分成列。在处理数据时,经常会遇到以元组形式存储的数据,其中包含多个值,例如(a, b)或(a, b, c)。在Pandas中,可以通过apply方法和无限制expand参数将元组中的值拆分成列。
阅读更多:Pandas 教程
准备数据
为了演示如何将元组中的字符串拆分成列,我们首先需要准备一些数据。假设我们有一个包含元组数据的DataFrame,其中包含两列,一列包含姓名和姓氏,另一列包含电子邮件和电话号码。我们可以使用以下代码创建这个DataFrame:
import pandas as pd
data = [
(('Tom', 'Hanks'), ('tom.hanks@gmail.com', '123-456-7890')),
(('Bruce', 'Wayne'), ('batman@wayneenterprises.com', '555-555-5555')),
(('Clark', 'Kent'), ('ckent@dailyplanet.com', '111-111-1111'))
]
df = pd.DataFrame(data, columns=['name', 'contact_info'])
print(df)
这将创建一个类似于以下内容的DataFrame:
name contact_info
0 (Tom, Hanks) (tom.hanks@gmail.com, 123-456-7890)
1 (Bruce, Wayne) (batman@wayneenterprises.com, 555-555-5555)
2 (Clark, Kent) (ckent@dailyplanet.com, 111-111-1111)
拆分元组
为了将元组中的字符串拆分成列,我们需要使用Pandas的apply方法和expand参数。apply方法用于将一个函数应用于整个DataFrame或某个列。我们可以在apply中定义一个lambda函数,使用expand参数将元组中的字符串拆分成列。以下是代码示例:
df[['First Name', 'Last Name']] = df['name'].apply(lambda x: pd.Series(x))
df[['Email', 'Phone']] = df['contact_info'].apply(lambda x: pd.Series(x))
print(df)
这将创建四个新列:First Name、Last Name、Email和Phone,它们包含将元组中的字符串拆分后得到的值。以下是结果:
name contact_info First Name Last Name Email Phone
0 (Tom, Hanks) (tom.hanks@gmail.com, 123-456-7890) Tom Hanks tom.hanks@gmail.com 123-456-7890
1 (Bruce, Wayne) (batman@wayneenterprises.com, 555-555-5555) Bruce Wayne batman@wayneenterprises.com 555-555-5555
2 (Clark, Kent) (ckent@dailyplanet.com, 111-111-1111) Clark Kent ckent@dailyplanet.com 111-111-1111
限制拆分
在某些情况下,我们可能只需要从元组中选择一个或几个值进行拆分,而不是全部拆分。我们可以在lambda函数中只返回所需的值,而不是返回完整的Series对象。以下是一些示例:
仅提取姓名
df[['First Name', 'Last Name']] = df['name'].apply(lambda x: pd.Series(x[:2]))
print(df)
这将提取姓名的第一部分和第二部分,忽略其他部分。以下是结果:
name contact_info First Name Last Name
0 (Tom, Hanks) (tom.hanks@gmail.com, 123-456-7890) Tom Hanks
1 (Bruce, Wayne) (batman@wayneenterprises.com, 555-555-5555) Bruce Wayne
2 (Clark, Kent) (ckent@dailyplanet.com, 111-111-1111) Clark Kent
``### 仅提取电子邮件
```python
df['Email'] = df['contact_info'].apply(lambda x: x[0])
print(df)
这将仅提取每个元组中的电子邮件。以下是结果:
name contact_info First Name Last Name Email Phone
0 (Tom, Hanks) (tom.hanks@gmail.com, 123-456-7890) Tom Hanks tom.hanks@gmail.com 123-456-7890
1 (Bruce, Wayne) (batman@wayneenterprises.com, 555-555-5555) Bruce Wayne batman@wayneenterprises.com 555-555-5555
2 (Clark, Kent) (ckent@dailyplanet.com, 111-111-1111) Clark Kent ckent@dailyplanet.com 111-111-1111
仅提取电话号码
df['Phone'] = df['contact_info'].apply(lambda x: x[1])
print(df)
这将仅提取每个元组中的电话号码。以下是结果:
name contact_info First Name Last Name Email Phone
0 (Tom, Hanks) (tom.hanks@gmail.com, 123-456-7890) Tom Hanks tom.hanks@gmail.com 123-456-7890
1 (Bruce, Wayne) (batman@wayneenterprises.com, 555-555-5555) Bruce Wayne batman@wayneenterprises.com 555-555-5555
2 (Clark, Kent) (ckent@dailyplanet.com, 111-111-1111) Clark Kent ckent@dailyplanet.com 111-111-1111
总结
在本文中,我们介绍了如何使用Pandas将元组中的字符串拆分成列。我们使用apply方法和expand参数将元组中的值拆分成列,并创建新的列来存储拆分后的结果。我们还演示了如何限制拆分,在lambda函数中仅返回所需的值。在处理包含元组数据的DataFrame时,将这些技术应用到实际数据中,可以帮助我们更轻松地提取和分析数据。
极客教程