合并两个数据框时的NaN值

时间:2019-10-16 19:39:04

标签: python

我知道这个问题已经问过很多遍了。但是以前的帖子似乎没有任何帮助解决我的问题。好吧,我不是很了解发生了什么。我有两个数据框,我想从中创建一个新的数据框。不知何故,我总是得到一个超级奇怪的结果,我不知道它从哪里来。

我的代码如下:


#calculate stock value

t = data['NetAmount'].sum()
data['Unit_cost']= round(data['NetAmount']/data['Quantity'])

#df2 is the dataframe that has the cost per unit for each sku

dff = pd.DataFrame(data['Unit_cost'])
dff1 = pd.DataFrame(data['Id'])
df2 = pd.concat([dff1,dff], axis=1)
df2.drop_duplicates(subset='Id',keep='first',inplace=True)
df3 = df2.sort_values(by='Id', ascending=True)

#preparer two lists to calculate the quantity of orders of each sku
list2 = data['Id'].values.tolist()
list3 = data['Quantity'].values.tolist()
data = data.values
idcol = data[:,0]

list1 = list(np.unique(idcol))

count = ({i:0 for i in list1})
for i in list2:
    if i in list1:
        count[i] += 1

#np count is a dictionary that includes the SKU and the occurance of the SKU in the dataset,

arr1=np.array(list2)
arr2= np.array(list3)

dict={}

for val in arr1:
    list3 = np.nonzero(arr1==val)
    dict[val]=arr2[list3[0]].sum()
for i in sorted (dict.keys()):
    S = i, dict[i]

# #construction of a new dataframe from the two dictionaries
s2= pd. DataFrame.from_dict([dict], orient='columns', dtype=None, columns=None)
s3 = s2.transpose()
s1 = pd.Series(count, index=count.keys())
sr = s2.append(s1, ignore_index=True)
sr1 = sr.transpose()



##add the two dataframe
sr2 = pd.concat([sr1, df3],axis=1)
print(sr2)

结果我得到一个表,看起来像这样:

0           NaN     NaN   10206.0       17.0
143         NaN     NaN   10245.0       16.0
694         NaN     NaN  103237.0       17.0
1625        NaN     NaN  103627.0       17.0
1792        NaN     NaN  103723.0       17.0
2321        NaN     NaN  123242.0      196.0
2354     7382.0  1231.0       NaN        NaN
2400        NaN     NaN  123723.0      205.0
2977        NaN     NaN  124437.0      210.0
3093        NaN     NaN  131645.0       13.0
3134     1107.0   134.0       NaN        NaN
3245    55918.0  4562.0       NaN        NaN
3318     1065.0   142.0       NaN        NaN
4244     6091.0  1355.0       NaN        NaN
4345     8956.0  1752.0       NaN        NaN
4527     5803.0  1316.0       NaN        NaN
4805        NaN     NaN  131848.0       14.0
4996        NaN     NaN  132127.0       13.0
5134     5423.0  1086.0       NaN        NaN
5245    14647.0  1965.0       NaN        NaN
6134    34390.0  2322.0       NaN        NaN
6206    12207.0  1292.0       NaN        NaN
6378        NaN     NaN  132218.0       14.0

有人会提示可以解决此问题吗?

0 个答案:

没有答案