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

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

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

问题

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

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

谢谢

  1. import pandas as pd
  2. data = [['CZK', '125.45', 1], ['CZK', '1726', 3], ['CZK', '135', 2], ['CZK', '2152.33', 3]]
  3. df = pd.DataFrame(data, columns=['currency', 'rate', 'nights'])
  4. #---------------
  5. data2 = [['CZK', '125.45', 1], ['CZK', '1726|1726|1726', 3], ['CZK', '135|135', 2], ['CZK', '2152.33|2152.33|2152.33', 3]]
  6. df2 = pd.DataFrame(data2, columns=['currency', 'rate', 'nights'])
  7. #---------------
  8. df['pipecount'] = (df['nights'] - 1) # 获取所需的管道数 "|";
  9. # df['Rate'] = ['|'.join(s[i:]) for i, s in zip(df['pipecount'], df['Daily Rate'].str.split('|'))]
  10. # df = df.drop(['pipecount'], axis=1)
  11. print("Before")
  12. print(df)
  13. print('\n')
  14. print("After")
  15. 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

  1. import pandas as pd
  2. data = [['CZK', '125.45', 1], ['CZK', '1726',3], ['CZK', '135',2],
  3. ['CZK','2152.33',3]]
  4. df = pd.DataFrame(data, columns=['currency', 'rate','nights'])
  5. #---------------
  6. data2 = [['CZK', '125.45',1], ['CZK', '1726|1726|1726',3], ['CZK',
  7. '135|135',2],['CZK','2152.33|2152.33|2152.33',3]]
  8. df2 = pd.DataFrame(data2, columns=['currency', 'rate','nights'])
  9. #---------------
  10. df['pipecount'] = (df['nights'] - 1) # Gets the number of PIPEs needed "|"
  11. #df['Rate'] = ['|'.join(s[i:]) for i, s in zip(df['pipecount'], df['Daily
  12. Rate'].str.split('|'))]
  13. #df = df.drop(['pipecount'], axis=1)
  14. print("Before")
  15. print(df)
  16. print('\n')
  17. print("After")
  18. print(df2)

答案1

得分: 2

你可以使用字符串操作:

  1. df['rate'] = df['rate'].astype(str).add('|').mul(df['nights']).str.strip('|')
  2. print(df)
  3. # 输出结果
  4. currency rate nights
  5. 0 CZK 125.45 1
  6. 1 CZK 1726|1726|1726 3
  7. 2 CZK 135|135 2
  8. 3 CZK 2152.33|2152.33|2152.33 3

细节部分:

  1. steps = pd.concat([
  2. out := df['rate'].astype(str).add('|'),
  3. out := out.mul(df['nights']),
  4. out := out.str.strip('|')
  5. ], axis=1, keys=['step1', 'step2', 'step3'])
  6. print(steps)
  7. # 输出结果
  8. step1 step2 step3
  9. 0 125.45| 125.45| 125.45
  10. 1 1726| 1726|1726|1726| 1726|1726|1726
  11. 2 135| 135|135| 135|135
  12. 3 2152.33| 2152.33|2152.33| 2152.33|2152.33|2152.33

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

英文:

You can use string manipulation:

  1. df['rate'] = df['rate'].astype(str).add('|').mul(df['nights']).str.strip('|')
  2. print(df)
  3. # Output
  4. currency rate nights
  5. 0 CZK 125.45 1
  6. 1 CZK 1726|1726|1726 3
  7. 2 CZK 135|135 2
  8. 3 CZK 2152.33|2152.33|2152.33 3

Details:

  1. steps = pd.concat([
  2. out := df['rate'].astype(str).add('|'),
  3. out := out.mul(df['nights']),
  4. out := out.str.strip('|')
  5. ], axis=1, keys=['step1', 'step2', 'step3'])
  6. print(steps)
  7. # Output
  8. step1 step2 step3
  9. 0 125.45| 125.45| 125.45
  10. 1 1726| 1726|1726|1726| 1726|1726|1726
  11. 2 135| 135|135| 135|135
  12. 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:

确定