gpt4 book ai didi

python - 每隔一行将数据从一列添加到另一列

转载 作者:行者123 更新时间:2023-12-01 23:20:36 29 4
gpt4 key购买 nike

我有两个数据框:

import pandas as pd
import numpy as np
sgRNA = pd.Series(["ABL1_sgABL1_130854834","ABL1_sgABL1_130862824","ABL1_sgABL1_130872883","ABL1_sgABL1_130884018"])
sequence = pd.Series(["CTTAGGCTATAATCACAATG","GGTTCATCATCATTCAACGG","TCAGTGATGATATAGAACGG","TTGCTCCCTCGAAAAGAGCG"])
df1=pd.DataFrame(sgRNA,columns=["sgRNA"])
df1["sequence"]=sequence

df2=pd.DataFrame(columns=["column"],
index=np.arange(len(df1) * 2))

我想每隔一行从 df1 到 df2 的两列中添加值,如下所示:

ABL1_sgABL1_130854834
CTTAGGCTATAATCACAATG
ABL1_sgABL1_130862824
GGTTCATCATCATTCAACGG
ABL1_sgABL1_130872883
TCAGTGATGATATAGAACGG
ABL1_sgABL1_130884018
TTGCTCCCTCGAAAAGAGCG

要为 df1["sgRNA"] 执行此操作,我使用了以下代码:

df2.iloc[0::2, :]=df1["sgRNA"]

但是我得到这个错误:ValueError:无法将输入数组从形状 (4,) 广播到形状 (4,1)。我做错了什么?

最佳答案

我认为您正在寻找 DataFrame.stack() :

df2["column"] = df1.stack().reset_index(drop=True)
print(df2)

打印:

                  column
0 ABL1_sgABL1_130854834
1 CTTAGGCTATAATCACAATG
2 ABL1_sgABL1_130862824
3 GGTTCATCATCATTCAACGG
4 ABL1_sgABL1_130872883
5 TCAGTGATGATATAGAACGG
6 ABL1_sgABL1_130884018
7 TTGCTCCCTCGAAAAGAGCG

关于python - 每隔一行将数据从一列添加到另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68146479/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com