连接/根据列值将字符串重复连接X次,以字符”|”(管道)分隔。

huangapple go评论118阅读模式
英文:

concatenate/ Join string onto itself X number of times based on column value separated by the character "|" (pipe)

问题

我正在处理一个谜题,在一个“rate”列中列出了一个金额值(字符串)。在处理这个速率列时,我需要将速率乘以“nights”列,并使用“|”管道字符分隔每个速率值(字符串)。永远不会在第一个或最后一个位置需要管道字符。

您可以提供的任何帮助,以使我能够继续前进,我将不胜感激。

谢谢

import pandas as pd

data = [['CZK', '125.45', 1], ['CZK', '1726', 3], ['CZK', '135', 2], ['CZK', '2152.33', 3]]

df = pd.DataFrame(data, columns=['currency', 'rate', 'nights'])

#--------------- 

data2 = [['CZK', '125.45', 1], ['CZK', '1726|1726|1726', 3], ['CZK', '135|135', 2], ['CZK', '2152.33|2152.33|2152.33', 3]]
df2 = pd.DataFrame(data2, columns=['currency', 'rate', 'nights'])

#---------------

df['pipecount'] = (df['nights'] - 1)  # 获取所需的管道数 "|";

# df['Rate'] = ['|'.join(s[i:]) for i, s in zip(df['pipecount'], df['Daily Rate'].str.split('|'))]

# df = df.drop(['pipecount'], axis=1)

print("Before")
print(df)
print('\n')
print("After")
print(df2)

请注意,我已经注释掉了一些代码,因为您的原始代码中有一些错误,并且部分代码与您的描述不匹配。您需要根据您的需求进行一些修正。

英文:

I'm working on a puzzle where I have one amount value(string) listed in a "rate" column. Working with the rate column, I need to multiply the rate times the "nights" column and separate each rate value(string) with a "|"pipe character. There will never be a need for the pipe character at the 1st or last position.

Any help that you can provide to get me moving forward I would appreciate it.

Thanks

import pandas as pd


data = [['CZK', '125.45', 1], ['CZK', '1726',3], ['CZK', '135',2], 
['CZK','2152.33',3]]


df = pd.DataFrame(data, columns=['currency', 'rate','nights'])

#--------------- 

data2 = [['CZK', '125.45',1], ['CZK', '1726|1726|1726',3], ['CZK', 
'135|135',2],['CZK','2152.33|2152.33|2152.33',3]]
df2 = pd.DataFrame(data2, columns=['currency', 'rate','nights'])

#---------------

df['pipecount'] = (df['nights'] - 1)    # Gets the number of PIPEs needed  "|"


#df['Rate'] = ['|'.join(s[i:]) for i, s in zip(df['pipecount'], df['Daily 
Rate'].str.split('|'))]


#df = df.drop(['pipecount'], axis=1)


print("Before")
print(df)
print('\n')
print("After")
print(df2)

答案1

得分: 2

你可以使用字符串操作:

df['rate'] = df['rate'].astype(str).add('|').mul(df['nights']).str.strip('|')
print(df)

# 输出结果
  currency        rate  nights
0      CZK      125.45       1
1      CZK  1726|1726|1726       3
2      CZK      135|135       2
3      CZK  2152.33|2152.33|2152.33       3

细节部分:

steps = pd.concat([
    out := df['rate'].astype(str).add('|'),
    out := out.mul(df['nights']),
    out := out.str.strip('|')
], axis=1, keys=['step1', 'step2', 'step3'])
print(steps)

# 输出结果
      step1               step2             step3
0   125.45|             125.45|           125.45
1     1726|  1726|1726|1726|  1726|1726|1726
2      135|            135|135|           135|135
3  2152.33|  2152.33|2152.33|  2152.33|2152.33|2152.33

请注意,这些是代码的翻译部分,不包括其他内容。

英文:

You can use string manipulation:

df['rate'] = df['rate'].astype(str).add('|').mul(df['nights']).str.strip('|')
print(df)

# Output
  currency                     rate  nights
0      CZK                   125.45       1
1      CZK           1726|1726|1726       3
2      CZK                  135|135       2
3      CZK  2152.33|2152.33|2152.33       3

Details:

steps = pd.concat([
    out := df['rate'].astype(str).add('|'),
    out := out.mul(df['nights']),
    out := out.str.strip('|')
], axis=1, keys=['step1', 'step2', 'step3'])
print(steps)

# Output
      step1                     step2                    step3
0   125.45|                   125.45|                   125.45
1     1726|           1726|1726|1726|           1726|1726|1726
2      135|                  135|135|                  135|135
3  2152.33|  2152.33|2152.33|2152.33|  2152.33|2152.33|2152.33

huangapple
  • 本文由 发表于 2023年6月13日 11:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定