简体   繁体   English

如何在 pandas 中使用 r 的 seq function

[英]how to use seq function of r in pandas

i want of sequence of values to be written on new column based on previous columns我希望根据以前的列将值序列写入新列

this is my dataframe df这是我的 dataframe df

a  b
11 15
32 35

i want output to be like this我希望 output 是这样的

a  b   c
11 15  11,12,13,14
32 35  32,33,34

it should get sequence from first two columns它应该从前两列获取序列

my code我的代码

df.apply(lambda x : range(x['a'],x['b']),1)

it gives me some thing like this not able to remove paranthesis and split by comma into multiple rows它给了我一些类似这样的东西,无法删除括号并用逗号分成多行

a  b   c
11 15  (11,12,13,14)
32 35  (32,33,34)

Since you mentioned you want to convert the values into rows you can do it from the generated tuple using explode .既然您提到要将值转换为行,您可以使用explode从生成的元组中执行此操作。 Otherwise @jezrael answer should work fine for you.否则@jezrael 答案应该适合你。

df = pd.DataFrame([[11, 15],[32, 35]],columns=['a','b'])
df['c']= df.apply(lambda x : range(x['a'],x['b']),1)

df.explode('c')

Out:出去:

    a   b   c
0  11  15  11
0  11  15  12
0  11  15  13
0  11  15  14
1  32  35  32
1  32  35  33
1  32  35  34

You can convert values to strings and join by join :您可以将值转换为字符串并通过join

df['c'] = df.apply(lambda x : ','.join(str(i) for i in range(x['a'],x['b'])),1)
#alternative
#df['c'] = [','.join(str(i) for i in range(x,y)) for x, y in df[['a','b']].to_numpy()]

Or:或者:

df['c'] = df.apply(lambda x : ','.join(map(str, range(x['a'],x['b']))),1)
#alternative
#df['c'] = [','.join(map(str, range(x,y))) for x, y in df[['a','b']].to_numpy()]

print (df)
    a   b            c
0  11  15  11,12,13,14
1  32  35     32,33,34

EDIT: For new rows use DataFrame.explode :编辑:对于新行使用DataFrame.explode

df['c']= df.apply(lambda x : range(x['a'],x['b']),1)

df = df.explode('c').reset_index(drop=True)

print (df)
    a   b   c
0  11  15  11
1  11  15  12
2  11  15  13
3  11  15  14
4  32  35  32
5  32  35  33
6  32  35  34

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

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