使用Python Pandas进行字符串操作

时间:2015-03-27 02:54:31

标签: string python-2.7 pandas

我有一些姓名和种族数据,例如:

John Wick    English
Black Widow  French

然后我做了一些操作,使名字如下所示

John Wick  -> john#wick??????????????????????????????????
Black Widow -> black#widow????????????????????????????????

然后我继续创建多个变量,每个变量都通过for循环包含3个字符的子字符串。

我也尝试使用re.findall找到字母数量。

我有两个问题: 1)for循环有效吗?即使它按原样工作,我可以用更好的代码替换吗? 2)我无法获得试图找到字母数量的代码。有什么建议吗?

import pandas as pd
from pandas import DataFrame
import re

# Get csv file into data frame
data = pd.read_csv("C:\Users\KubiK\Desktop\OddNames_sampleData.csv")
frame = DataFrame(data)
frame.columns = ["name", "ethnicity"]
name = frame.name
ethnicity = frame.ethnicity

# Remove missing ethnicity data cases
index_missEthnic = frame.ethnicity.isnull()
index_missName = frame.name.isnull()
frame2 = frame.loc[~index_missEthnic, :]
frame3 = frame2.loc[~index_missName, :]

# Make all letters into lowercase
frame3.loc[:, "name"] = frame3["name"].str.lower()
frame3.loc[:, "ethnicity"] = frame3["ethnicity"].str.lower()

# Remove all non-alphabetical characters in Name
frame3.loc[:, "name"] = frame3["name"].str.replace(r'[^a-zA-Z\s\-]', '') # Retain space and hyphen

# Replace empty space as "#"
frame3.loc[:, "name"] = frame3["name"].str.replace('[\s]', '#')

# Find the longest name in the dataset
##frame3["name_length"] = frame3["name"].str.len()
##nameLength = frame3.name_length
##print nameLength.max() # Longest name has !!!40 characters!!! including spaces and hyphens

# Add "?" to fill spaces up to 43 characters
frame3["name_filled"] = frame3["name"].str.pad(side="right", width=43, fillchar="?")

# Split into three-character strings
for i in range(1, 41):
    substr = "substr" + str(i)
    frame3[substr] = frame3["name_filled"].str[i-1:i+2]

# Count number of characters
frame3["name_len"] = len(re.findall('[a-zA-Z]', name))

# Test outputs
print frame3

1 个答案:

答案 0 :(得分:1)

!)关于循环,我想不出比你正在做的更好的方式

2)尝试frame3["name_len"] = frame3["name"].map(lambda x : len(re.findall('[a-zA-Z]', x)))