📜  Pandas 数据帧中的字符串处理

📅  最后修改于: 2022-05-13 01:55:37.241000             🧑  作者: Mango

Pandas 数据帧中的字符串处理

在本文中,我们将学习 Pandas Dataframe 中的 String Munging。 Munging 被称为通过转换来清理任何杂乱的东西。用技术术语来说,我们可以说将数据库中的数据转换成有用的形式。

示例:“ no-one@example.com”,变成“no-one at example dot com”

方法:

第一步:导入库

Python3
import pandas as pd
import numpy as np
import re as re


Python3
raw_data = {"first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"],
            "last_name": ["Miller", "Jacobson", "Ali", "Milner", "Cooze"],
            "email": ["jas203@gmail.com", "momomolly@gmail.com", np.NAN,
                      "battler@milner.com", "Ames1234@yahoo.com"]}
  
df = pd.DataFrame(raw_data, columns=["first_name", "last_name", "email"])
print()
print(df)


Python3
print(df["email"].str.contains("gmail"))


Python3
pattern = "([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})"
print(df["email"].str.findall(pattern, flags=re.IGNORECASE))


Python3
def ProjectPro_Ex_136():
  
    print()
    print('**How we can do string munging in Pandas**')
  
    # loading libraries
    import pandas as pd
    import numpy as np
    import re as re
  
    # Creating dataframe
    raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                'email': ['jas203@gmail.com', 'momomolly@gmail.com', np.NAN,
                          'battler@milner.com', 'Ames1234@yahoo.com']}
  
    df = pd.DataFrame(raw_data, columns=['first_name', 'last_name', 'email'])
    print()
    print(df)
  
    # Let us find Which string within the 
    # email column contains ‘gmail’
    print()
    print(df['email'].str.contains('gmail'))
  
    # Create a daily expression pattern that
    # breaks apart emails
    pattern = '([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\\.([A-Z]{2,4})'
  
    # Find everything in df.email that contains
    # that pattern
    print()
    print(df['email'].str.findall(pattern, flags=re.IGNORECASE))
  
  
ProjectPro_Ex_136()


第 2 步:创建数据框

现在创建一个字典并通过 pd.DataFrame 传递它来创建一个 Dataframe。

蟒蛇3

raw_data = {"first_name": ["Jason", "Molly", "Tina", "Jake", "Amy"],
            "last_name": ["Miller", "Jacobson", "Ali", "Milner", "Cooze"],
            "email": ["jas203@gmail.com", "momomolly@gmail.com", np.NAN,
                      "battler@milner.com", "Ames1234@yahoo.com"]}
  
df = pd.DataFrame(raw_data, columns=["first_name", "last_name", "email"])
print()
print(df)

第 3 步:应用不同的 Munging 操作

首先,检查功能“email”中哪个字符串包含“Gmail”。

蟒蛇3

print(df["email"].str.contains("gmail"))

现在我们想要将电子邮件分成几部分,使得“@”之前的字符成为一个字符串,“.”之后和之前的字符成为一个字符串。成为一。最后,剩下的就变成了 one 字符串。

蟒蛇3

pattern = "([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})"
print(df["email"].str.findall(pattern, flags=re.IGNORECASE))

下面是实现:

蟒蛇3

def ProjectPro_Ex_136():
  
    print()
    print('**How we can do string munging in Pandas**')
  
    # loading libraries
    import pandas as pd
    import numpy as np
    import re as re
  
    # Creating dataframe
    raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
                'email': ['jas203@gmail.com', 'momomolly@gmail.com', np.NAN,
                          'battler@milner.com', 'Ames1234@yahoo.com']}
  
    df = pd.DataFrame(raw_data, columns=['first_name', 'last_name', 'email'])
    print()
    print(df)
  
    # Let us find Which string within the 
    # email column contains ‘gmail’
    print()
    print(df['email'].str.contains('gmail'))
  
    # Create a daily expression pattern that
    # breaks apart emails
    pattern = '([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\\.([A-Z]{2,4})'
  
    # Find everything in df.email that contains
    # that pattern
    print()
    print(df['email'].str.findall(pattern, flags=re.IGNORECASE))
  
  
ProjectPro_Ex_136()

输出: