Excel,如何通过逗号分隔符将单元格拆分为新单元格
Excel, How to split cells by comma delimiter into new cells
假设我有这样的数据,带有一些分隔符(如逗号),我想将其拆分为新单元格,跨列或向下拆分为行。
The Data
Location
One Museum, Two Museum
City A
3rd Park, 4th Park, 5th Park
City B
在任何一个方向上你会怎么做?方法很多,为什么优先提供方法?
Excel 手动方法: 单击“数据”>“文本到列”。如果您想要一列中的数据,现在只需复制并过去。这仅在数据集较小且您只执行一次时才有用。
Power Query方法:这个方法你对数据源做一次,以后数据变化的时候点刷新按钮。数据源几乎可以是 csv、 等任何内容。步骤如下:
1 - 选择您的数据源
2 - 在 excel 范围内选择 From Table/ Range
3 - 现在选择拆分方式,有分隔符,还有其他6种选择。
4 - 对于此数据,我在自定义时使用 ", "
5 & 6 - 要拆分,您必须 select 高级选项。制作select离子。
7 关闭并加载
这是一个很好的方法,因为除非您愿意,否则不必在 Power Query 中编写代码。
Python方法
确保你已经安装了 pip pandas 或使用 conda 安装 pandas.
代码是这样的:
import pandas as pd
df = pd.read_excel('path/to/myexcelfile.xlsx')
df[['key.0','key.1','key.2']] = df['The Data'].str.split(',', expand=True)
df.drop(columns=['The Data'], inplace = True)
# stop here if you want the data to be split into new columns
数据是这样的
Location key.0 key.1 key.2
0 City A One Museum Two Museum None
1 City B 3rd park 4th park 5th park
要拆分成行,请继续执行下一个代码部分:
stacked = df.set_index('Location').stack()
# set the name of the new series created
df = stacked.reset_index(name='The Data')
# drop the 'source' level (key.*)
df.drop('level_1', axis=1, inplace=True)
现在完成了,看起来像这样
Location The Data
0 City A One Museum
1 City A Two Museum
2 City B 3rd park
3 City B 4th park
4 City B 5th park
python 的好处是对于较大的数据集来说速度更快,您可以使用正则表达式以可能的 100 种方式进行拆分。数据源可以是您将用于高级查询等的所有类型。
R
library(data.table)
dt <- fread("yourfile.csv") # or use readxl package for xls files
dt
# Data Location
# 1: One Museum, Two Museum City A
# 2: 3rd Park, 4th Park, 5th Park City B
dt[, .(Data = unlist(strsplit(Data, ", "))), by = Location]
# Location Data
# 1: City A One Museum
# 2: City A Two Museum
# 3: City B 3rd Park
# 4: City B 4th Park
# 5: City B 5th Park
假设我有这样的数据,带有一些分隔符(如逗号),我想将其拆分为新单元格,跨列或向下拆分为行。
The Data | Location |
---|---|
One Museum, Two Museum | City A |
3rd Park, 4th Park, 5th Park | City B |
在任何一个方向上你会怎么做?方法很多,为什么优先提供方法?
Excel 手动方法: 单击“数据”>“文本到列”。如果您想要一列中的数据,现在只需复制并过去。这仅在数据集较小且您只执行一次时才有用。
Power Query方法:这个方法你对数据源做一次,以后数据变化的时候点刷新按钮。数据源几乎可以是 csv、
1 - 选择您的数据源
2 - 在 excel 范围内选择 From Table/ Range
3 - 现在选择拆分方式,有分隔符,还有其他6种选择。
4 - 对于此数据,我在自定义时使用 ", "
5 & 6 - 要拆分,您必须 select 高级选项。制作select离子。
7 关闭并加载
这是一个很好的方法,因为除非您愿意,否则不必在 Power Query 中编写代码。
Python方法 确保你已经安装了 pip pandas 或使用 conda 安装 pandas.
代码是这样的:
import pandas as pd
df = pd.read_excel('path/to/myexcelfile.xlsx')
df[['key.0','key.1','key.2']] = df['The Data'].str.split(',', expand=True)
df.drop(columns=['The Data'], inplace = True)
# stop here if you want the data to be split into new columns
数据是这样的
Location key.0 key.1 key.2
0 City A One Museum Two Museum None
1 City B 3rd park 4th park 5th park
要拆分成行,请继续执行下一个代码部分:
stacked = df.set_index('Location').stack()
# set the name of the new series created
df = stacked.reset_index(name='The Data')
# drop the 'source' level (key.*)
df.drop('level_1', axis=1, inplace=True)
现在完成了,看起来像这样
Location The Data
0 City A One Museum
1 City A Two Museum
2 City B 3rd park
3 City B 4th park
4 City B 5th park
python 的好处是对于较大的数据集来说速度更快,您可以使用正则表达式以可能的 100 种方式进行拆分。数据源可以是您将用于高级查询等的所有类型。
R
library(data.table)
dt <- fread("yourfile.csv") # or use readxl package for xls files
dt
# Data Location
# 1: One Museum, Two Museum City A
# 2: 3rd Park, 4th Park, 5th Park City B
dt[, .(Data = unlist(strsplit(Data, ", "))), by = Location]
# Location Data
# 1: City A One Museum
# 2: City A Two Museum
# 3: City B 3rd Park
# 4: City B 4th Park
# 5: City B 5th Park