【问题标题】:Splitting text data into multiple columns using Regex使用正则表达式将文本数据拆分为多列
【发布时间】:2019-09-19 03:12:44
【问题描述】:

我是正则表达式的新手,我想将一些文本数据拆分成列。查看“测试数据”,结构是:名字/姓氏、大学和国家。如何将此文本分成三列(每列包含姓名、大学和国家)?

test_data = "Bob Smith, São Paulo State University/Department of Production Engineering, Brazil James Smith, São Paulo State University/Department of Production Engineering, Brazil Bob James, São Paulo State University/Department of Production Engineering, Brazil"

test_df = pd.DataFrame([test_data], columns=["test_data"])
split_df = test_df["test_data"].str.split(r'\w+,', expand=True)
split_df.head()

提前致谢!

【问题讨论】:

  • 你的测试数据不好。为什么?两个条目之间没有分隔符。
  • 你是对的,但是逗号不能用来分隔数据吗?所有条目都遵循相同的名称、逗号、大学、逗号和国家格式。谢谢。
  • 每个国家后面都少了一个逗号,所以如果你尝试用逗号分割,你最终会在同一个列表元素中得到国家和下一个名字

标签: python regex python-3.x pandas


【解决方案1】:

我不确定您是如何生成输入数据的,我也不确定数据在更大的集合中是否一致。这个答案是基于当前的数据集结构没有修改。您应该能够将最终输出添加到数据框。如果您对此有疑问,我也会添加该部分。

from pprint import pprint

input_string = 'Bob Smith, São Paulo State University/Department of Production Engineering, Brazil James Smith, São Paulo State University/Department of Production Engineering, Brazil Bob James, São Paulo State University/Department of Production Engineering, Brazil'

def split_string_keep_delimiter(string_to_split, delimiter):
  result_list = []
  tokens = string_to_split.split(delimiter)
  for i in range(len(tokens) - 1):
    result_list.append(tokens[i] + delimiter)
  result_list.append(tokens[len(tokens)-1])
  return  result_list

# This is going to split your input text on the word Brazil
# the output is a list
split_input = split_string_keep_delimiter(input_string, "Brazil")
pprint(split_input)
# output
['Bob Smith, São Paulo State University/Department of Production '
'Engineering,Brazil',
'James Smith, São Paulo State University/Department of Production '
'Engineering,Brazil',
'Bob James, São Paulo State University/Department of Production '
'Engineering,Brazil',
'']

# This is going to split the previous list at the commas (,).
# the output is a nested list
results = [item.split(',') for item in split_input if len(item) > 0]
print (results)
# output
[['Bob Smith', ' São Paulo State University/Department of Production Engineering', ' Brazil'], [' James Smith', ' São Paulo State University/Department of Production Engineering', ' Brazil'], [' Bob James', ' São Paulo State University/Department of Production Engineering', ' Brazil']]

# This loops through the results and extracts 4 items from each list.
for item in results:
  name = item[0].strip()
  university_name = item[1].strip().split('/')[0]
  department = item[1].strip().split('/')[1]
  country = item[2].strip()
  print (f'{name} - {university_name} - {department} - {country}')
  # output
  Bob Smith - São Paulo State University - Department of Production Engineering - Brazil
  James Smith - São Paulo State University - Department of Production Engineering - Brazil
  Bob James - São Paulo State University - Department of Production Engineering - Brazil

【讨论】:

  • 谢谢 - 我能够使用它。我确实想出了一个似乎适用于我的大部分数据集的替代方案:split_df = test_df["test_data"].str.split(r'(.*?,.*?,\s\w+)', expand=True)
【解决方案2】:

如果您的数据结构更好,每列由“,”分隔,那么您可以执行以下操作。

在:

test_data = "São Paulo State University/Department of Production Engineering, Brazil, James Smith, São Paulo State University/Department of Production Engineering, Brazil, Bob James, São Paulo State University/Department of Production Engineering, Brazil, Mike Smith"

df = pd.DataFrame(data = np.array(test_data.split(',')).reshape(-1, 3), columns = ['University', 'Country', 'Name'])

输出:

|   |                            University                           | Country | Name        |
|---|:---------------------------------------------------------------:|---------|-------------|
| 0 | São Paulo State University/Department of Production Engineering | Brazil  | James Smith |
| 1 | São Paulo State University/Department of Production Engineering | Brazil  | Bob James   |
| 2 | São Paulo State University/Department of Production Engineering | Brazil  | Mike Smith  |

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-30
    • 2023-03-23
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2018-04-06
    • 2022-01-17
    相关资源
    最近更新 更多