简体   繁体   English

R 序列 function 在 Python

[英]R sequence function in Python

pandas version: 1.2 pandas 版本:1.2

I am trying to take a python pandas dataframe column pandas and create the same type of logic as in R that would be我正在尝试采用 python pandas dataframe 列 pandas 并创建与 R 中相同类型的逻辑

ss=sequence(df$los)

Which produces for the first two records产生前两个记录

[1]  1  2  3  4  5  1  2  3  4  5 

Example dataframe:示例 dataframe:

df = pd.DataFrame([('test', 5), ('t2', 5), ('t3', 2), ('t4', 6)],
                  columns=['first', 'los'])
df

  first  los
0  test    5
1    t2    5
2    t3    2
3    t4    6

So the first row is sequenced 1-5 and second row is sequenced 1-5 and third row is sequenced 1-2 etc. In R this becomes one sequenced list.因此,第一行的顺序为 1-5,第二行的顺序为 1-5,第三行的顺序为 1-2,依此类推。在 R 中,这变成了一个序列表。 I would like that is python.我想那是 python。

What I have been able to do is.我能做的是。

ss = df['los']
ss.apply(lambda x: np.array(range(1, x)))
18                          [1, 2, 3, 4, 5]
90                          [1, 2, 3, 4, 5]
105                                   [1,2]
106                      [1, 2, 3, 4, 5, 6]

Which is close but then I need to combine it into a single pd.Series so that it should be:这很接近但是我需要将它组合成一个 pd.Series 以便它应该是:

[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 1, 2, 3, 4, 5, 6]

You can just use concatenate :你可以只使用concatenate

np.concatenate([np.arange(x)+1 for x in df['los']])

Output: Output:

array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 1, 2, 3, 4, 5, 6])

Use explode() :使用explode()

df.los.apply(lambda x: np.arange(1, x+1)).explode().tolist()

Output: Output:

[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 1, 2, 3, 4, 5, 6]

Note - you can skip the ss assignment step, and use np.arange to streamline a bit.注意 - 您可以跳过ss分配步骤,并使用np.arange来简化一点。

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

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