I am attempting to left merge two dataframes, but I am running into an issue. I get only NaN's in columns that are in the right dataframe.
This is what I did:
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);
Here's the output:
-----FILE DATA-----
Chrom Gene Position
0 20 DZANK1 18446022
1 20 TGM6 2380332
2 20 C20orf96 271226
-----INTERNALS-----
{'_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': {}}
-----FILE DATA-----
Chrom Position Random
0 20 18446022 ABC
1 20 2380332 XYZ
2 20 271226 PQR
-----INTERNALS-----
{'_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
As you see, there's a column of NaN's there where there should be values from Random column in Y. Any ideas on how to debug this?
Working for me (v0.10.0b1, though I am somewhat confident--but haven't checked-- this would also work in 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
I'm very surprised that all the columns are object dtype. There must be some kind of parsing problem with your data-- examine the values in each column (not what they look like, but what they actually are , strings, ints, what?)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.