1 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
如果我正確理解你的問(wèn)題,那么當(dāng)你創(chuàng)建 empty_numpy 矩陣時(shí)你的問(wèn)題就開始了。我最喜歡的解決方案是使用empty_numpy = np.empty([5,5])代替(這里的默認(rèn) dtypes 是 float64)。那么“第一次嘗試的結(jié)果:”是正確的。它的意思是:
import pandas as pd
import numpy as np
dataset = [[[i for i in range(5)],] for i in range(5)]
dataset = pd.DataFrame(dataset, columns=['test'])
empty_numpy = np.empty([5,5])
# here you may add empty_numpy.fill(np.nan) but it's not necessary,result is the same
empty_frame = pd.DataFrame(empty_numpy, columns=[str(i) for i in range(5)])
series = dataset['test']
target_frame = pd.DataFrame(list(series))
# following assignment is correct then
empty_frame[:] = target_frame
print('='*40)
print(f'Data types of empty_frame: {empty_frame.dtypes}')
print('='*40)
print("Result of first try: ")
print(empty_frame)
print("="*40)
或者只是將 dtype 屬性添加到您的 np.arrange 調(diào)用中,就像這樣:
empty_numpy = np.arange(25, dtype=float).reshape(5, 5)
然后它也可以工作(但有點(diǎn)無(wú)聊;o)。
添加回答
舉報(bào)