- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要根据字符 '/' 拆分单词并以这种方式改造单词:
这个数据框包含一些 child 和他们的复活节礼物。有些 child 有两件礼物,而有些 child 只有一件。
data = {'Presents':['Pink Doll / Ball', 'Bear/ Ball', 'Barbie', 'Blue Sunglasses/Airplane', 'Orange Kitchen/Car', 'Bear/Doll', 'Purple Game'],
'Kids': ['Chris', 'Jane', 'Betty', 'Harry', 'Claire', 'Sofia', 'Alex']
}
df = pd.DataFrame (data, columns = ['Presents', 'Kids'])
print (df)
这个数据框看起来像这样:
Presents Kids
0 Pink Doll / Ball Chris
1 Bear/ Ball Jane
2 Barbie Betty
3 Blue Sunglasses/Airplane Harry
4 Orange Kitchen/Car Claire
5 Bear/Doll Sofia
6 Purple Game Alex
我试图划定他们的礼物,并以这种方式改造他们,保持他们相关的颜色:
'Pink Doll/Ball'
将分为两部分:
'Pink Doll'
,
'Pink Ball'
.除此之外,同一个 child 应该与他们的礼物相关联。
Presents Kids
0 Pink Doll Chris
1 Pink Ball Chris
2 Bear Jane
3 Ball Jane
4 Barbie Betty
5 Blue Sunglasses Harry
6 Blue Airplane Harry
7 Orange Kitchen Claire
8 Orange Car Claire
9 Bear Sofia
10 Doll Sofia
11 Purple Game Alex
我的第一种方法是将列转换为列表并使用列表。像这样:
def count_total_words(string):
total = 1
for i in range(len(string)):
if (string[i] == ' '):
total = total + 1
return total
coloured_presents_to_remove_list = []
index_with_slash_list = []
first_present = ''
second_present= ''
index_with_slash = -1
refactored_second_present = ''
for coloured_present in coloured_presents_list:
if (coloured_present.find('/') >= 0):
index_with_slash = coloured_presents_list.index(coloured_present)
index_with_slash_list.append(index_with_slash)
first_present, second_present = coloured_present.split('/')
coloured_presents_to_remove_list.append(coloured_present)
if count_total_words(first_present) == 2:
refactored_second_present = first_present.split(' ', 1)[0] + ' ' + second_present
second_present = refactored_second_present
coloured_presents_list.append(first_present)
coloured_presents_list.append(second_present)
kids_list.insert(coloured_presents_list.index(first_present), kids_list[index_with_slash])
kids_list.insert(coloured_presents_list.index(second_present), kids_list[index_with_slash])
for present in coloured_presents_to_remove_list:
coloured_presents_list.remove(present)
for index in index_with_slash_list:
kids_list.pop(index)
但是,我意识到在某些时候,我可能会错误地丢失一些索引,因此我尝试将 Pandas 用于数据帧。
mask = df['Presents'].str.contains('/', na=False, regex=False)
df['First Present'], df['Second Present'] = df.loc[mask, 'Presents'].split('/')
最佳答案
您可以使用 str.split
使用 regex
与 expand=True
得到你的第一个和第二个礼物。请注意,这将处理三种情况 'present1/present2'
, 'coulour present'
和 'present'
.在后两种情况下,新创建的列 'present2'
将是 None
.
办案'colour present1/present2'
您可以使用 str.extract
使用包含允许颜色的正则表达式(参见下面的 colours_regex
)。这是为了区分颜色与由两个单词组成的礼物(例如 'Barby Doll'
)。
最后一步是使用 melt
与 'Kids'
作为标识符
df[['present1', 'present2']] = df.Presents.str.split('\s*/\s*', expand=True)
colours_regex = '(Blue|Purple|Pink|Orange)' # maybe not ideal if there are vast amounts of colours as this needs updating for every colour
df['colour'] = df.present1.str.extract(colours_regex)
df.loc[df.colour.notnull()&df.present2.notnull(), 'present2'] = df.loc[df.colour.notnull()&df.present2.notnull(), ['colour', 'present2']].agg(' '.join, axis=1)
result = df.melt(id_vars='Kids', value_vars=['present1', 'present2'], value_name='Present')
result = result.loc[result.Present.notnull(), ['Present', 'Kids']]
关于python - 如何按 '/' 拆分字符串并通过数据帧中的拆分子字符串对其进行重组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66889932/
使用 threejs,我尝试使用以下示例: http://threejs.org/examples/#css3d_molecules 问题是在那个例子中,从灰色球到红色球的键应该是双键。我发现了一些建
我有一个分子测试,可以启动 2 个 Docker 容器,用于一次测试 2 个应用程序版本。 dependency: name: galaxy driver: name: docker lint
假设我们依次应用了 3 个过滤器: b, a = iirfilter(...) # or bilinear(...) or anything else producing b, a y = lfil
在显式传递命令后,服务模块似乎没有启动我的 docker 守护进程。 Ansible 未在目标主机中启动 docker 守护进程。任务: - name: Install Docker apt:
我见过有人使用 pip install docker-py 或 pip install 'molecule[docker]'。 我相信它们是相似的(等价的?) 我读了https://molecule.
我是一名优秀的程序员,十分优秀!