簡體   English   中英

在熊貓中合並兩個數據框的問題

[英]Issue with merging two dataframes in Pandas

我試圖離開合並兩個數據幀,但我遇到了一個問題。 我在正確的數據框中的列中僅獲得NaN。

這是我所做的:

X = read_csv('fileA.txt',sep=',',header=0);
print "-----FILE DATA-----"
print X;
X = X.astype(object); # convert every column to string type? does it do it?
print "-----INTERNALS-----"
pprint(vars(X));

Y = file_to_dataframe('fileB.txt',',',0); 
print "-----FILE DATA-----"
print Y;
print "-----INTERNALS-----"
pprint(vars(Y));

Z = merge(X,Y,how='left');
print Z;
sys.exit(); 

Y = file_to_dataframe('tmp.chr20.thresh.frq.count','\t',0);
print Y.dtypes;

def file_to_dataframe(filename,sep,header): # list of dict's
    i = 0; k = 0;
    cols = list();
    colNames = list();
    for line in fileinput.input([filename]):
        line = line.rstrip('\n');
        lst = line.split(sep);
        if i == header: #  row number to use as the column names
            for colName in lst:
                colNames.append(colName);
        elif i > header:
            j = 0;
            record = dict();
            for j in range(0,len(lst)): # iterate over all tokens in the current line
                if j >= len(colNames):
                    colNames.append('#Auto_Generated_Label_'+ str(k));
                    k += 1;
                record[colNames[j]] = lst[j];
            cols.append(record); # push the record onto stack
        i += 1;
    return DataFrame.from_records(cols);

這是輸出:

-----文件數據-----

   Chrom      Gene  Position


0     20    DZANK1  18446022


1     20      TGM6   2380332


2     20  C20orf96    271226

-----內部-

{'_data': BlockManager


Items: array([Chrom, Gene, Position], dtype=object)


Axis 1: array([0, 1, 2])


ObjectBlock: array([Chrom, Gene, Position], dtype=object), 3 x 3, dtype object,


 '_item_cache': {}}

-----文件數據-----

  Chrom  Position Random


0    20  18446022    ABC


1    20   2380332    XYZ


2    20    271226    PQR

-----內部-

{'_data': BlockManager


Items: array([Chrom, Position, Random], dtype=object)


Axis 1: array([0, 1, 2])


ObjectBlock: array([Chrom, Position, Random], dtype=object), 3 x 3, dtype object,


 '_item_cache': {}}



  Chrom      Gene  Position Random

0    20  C20orf96    271226    NaN

1    20      TGM6   2380332    NaN

2    20    DZANK1  18446022    NaN

如您所見,在NaN的列中,Y的Random列中應該有值。有關如何調試此值的任何想法?

為我工作(v0.10.0b1,雖然我有一點信心-但尚未檢查-在0.9.1中也可以工作):

In [7]: x
Out[7]: 
   Chrom      Gene  Position
0     20    DZANK1  18446022
1     20      TGM6   2380332
2     20  C20orf96    271226

In [8]: y
Out[8]: 
   Chrom  Position Random
0     20  18446022    ABC
1     20   2380332    XYZ
2     20    271226    PQR

In [9]: pd.merge(x, y, how='left')
Out[9]: 
   Chrom      Gene  Position Random
0     20    DZANK1  18446022    ABC
1     20      TGM6   2380332    XYZ
2     20  C20orf96    271226    PQR

我很驚訝所有列都是對象dtype。 必須有某種解析問題與您的數據-在每一列(不是它們是什么樣子,但實際上他們 ,字符串,整數,是什么?)檢查值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM