简体   繁体   English

如何根据列中的数值创建逗号分隔的数字列表

[英]How to create comma separated list of numbers based on numeric value in column

I am trying to figure out how to create a comma separated list of numbers based on a numeric value in a specific column.我试图弄清楚如何根据特定列中的数值创建逗号分隔的数字列表。 For example, if the numeric column has a value of 5, I want to create a comma separated list of numbers in another column as "1, 2, 3, 4, 5".例如,如果数字列的值为 5,我想在另一列中创建一个逗号分隔的数字列表,如“1、2、3、4、5”。

Assuming we start with this dataframe:假设我们从这个 dataframe 开始:

  inventory_partner inventory_partner2  calc
0                A1                 aa     1
1                A2                 bb     2
2                A3                 cc     5
3                A4                 dd     4
4                A5                 ee     5
5                A6                 ff     3

I'm trying to get to this dataframe without having to manually code in the lists for each calc numeric possibility:我试图得到这个 dataframe 而不必在列表中手动编码每个计算数字的可能性:

  inventory_partner inventory_partner2  calc  my_comma_list
0                A1                 aa     1              1
1                A2                 bb     2           1, 2
2                A3                 cc     5  1, 2, 3, 4, 5
3                A4                 dd     4     1, 2, 3, 4
4                A5                 ee     5  1, 2, 3, 4, 5
5                A6                 ff     3        1, 2, 3

Below is my code that is manually creating a numeric list to apply based on each calc value.下面是我的代码,它根据每个计算值手动创建要应用的数字列表。 Is there a simpler way of doing this using a for loop?有没有更简单的方法使用 for 循环来做到这一点? I tried it in the code below but I couldn't fetch the calc value to use in the for loop.我在下面的代码中进行了尝试,但无法获取要在 for 循环中使用的 calc 值。

#create dataframe
d = {'inventory_partner': ['A1', 'A2', 'A3', 'A4', 'A5', 'A6'], 'inventory_partner2': ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'], 'calc': [1, 2, 5, 4, 5, 3]}
df1 = pd.DataFrame(data=d)

print(df1) #print original dataframe

#create my_comma_list column based on number values in calc column - too much code 
df1.insert(3, 'my_comma_list', '')
df1.loc[df1['calc'] == 1, 'my_comma_list'] = '1'
df1.loc[df1['calc'] == 2, 'my_comma_list'] = '1, 2'
df1.loc[df1['calc'] == 3, 'my_comma_list'] = '1, 2, 3'
df1.loc[df1['calc'] == 4, 'my_comma_list'] = '1, 2, 3, 4'
df1.loc[df1['calc'] == 5, 'my_comma_list'] = '1, 2, 3, 4, 5'

#df1['inventory_partner'] = ",".join([str(i) for i in range(0, df1['calc'].values)]) #I tried something like this but it can't fetch the calc value and use it in the for loop

print(df1) 

You can use df.apply to apply str.join on range object created from 'calc' :您可以使用df.applystr.join应用于从'calc'创建的range object :

>>> df['my_comma_list'] = df.calc.apply(lambda x: ', '.join(map(str, range(1, x+1))))

  inventory_partner inventory_partner2  calc  my_comma_list
0                A1                 aa     1              1
1                A2                 bb     2           1, 2
2                A3                 cc     5  1, 2, 3, 4, 5
3                A4                 dd     4     1, 2, 3, 4
4                A5                 ee     5  1, 2, 3, 4, 5
5                A6                 ff     3        1, 2, 3

Try apply with .join :尝试使用.join apply

 df['my_comm_list'] = df['calc'].apply(lambda x: ', '.join(np.arange(1,x+1).astype(str)) )

You can use the apply function and create the response in a separate function, so you have it isolated.您可以使用应用 function 并在单独的 function 中创建响应,因此您可以将其隔离。

import pandas as pd


d = {'inventory_partner': ['A1', 'A2', 'A3', 'A4', 'A5', 'A6'], 'inventory_partner2': ['aa', 'bb', 'cc', 'dd', 'ee', 'ff'], 'calc': [1, 2, 5, 4, 5, 3]}
df1 = pd.DataFrame(data=d)

print(df1) #print original dataframe

def create_comma_list(number):
  res = []
  for n in range(1,number+1):
    res.append(str(n))
   
  return ", ".join(res)

df1["my_comma_list"] = df1.apply(lambda x: create_comma_list(x.calc), axis=1)

print(df1) 

Source: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html资料来源: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.ZFC35FDC70D5FC69D269883A822C7A53E

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM