Python 3:导入浮动文件并为其绘制唯一标识符

问题描述 投票:1回答:1

我有两个名为“Posterior_C.txt”和“Posterior_l.txt”的文件,每个文件包含5000个浮动条目,我想导入并连接到数据框(用于在seaborn中绘图)。属于Posterior_C的每个条目应该被赋予标签C,并且属于Posterior_l的每个条目应该被称为l。

如何导入数据并连接它们,同时为每个数据创建唯一标识符。例如。

0.012 Posterior_C
0.0021 Posterior_C
0.2 Posterior_l
0.52 Posterior_l

这是我到目前为止所得到的:

import pandas as pd
import numpy as np

C=np.loadtxt("Posterior_C.txt")
l=np.loadtxt("Posterior_l.txt")
df={C,l}
df=pd.DataFrame(df)

import numpy as np

xc = np.array(["C"])
c=np.repeat(xc, 5000, axis=0)
import numpy as np

xl = np.array(["l"])
l=np.repeat(xl, 5000, axis=0)

但现在有点卡住了。

*在R我会做*

C<-read.table("Posterior_C.txt,header=FALSE)
l<-read.table("Posterior_l.txt,header=FALSE)
df=rbind(C,l)  
df<-as.data.frame(df)
dfID=rbind(rep("C",NROW(C),rep("l",NROW(l))
df$ID<-cbind(df,dfID[,1] )

或类似的东西。

python-3.x numpy seaborn
1个回答
1
投票

像这样的东西:

c = pd.read_table("Posterior_C.txt", header=None)
l = pd.read_table("Posterior_l.txt", header=None)
c['ID'] = 'C'
l['ID'] = 'l'
df = pd.concat([c, l], ignore_index=True)
© www.soinside.com 2019 - 2024. All rights reserved.