我有一個這樣的元組串列:
[
(12, Timestamp(...), 'test_1'),
(14, Timestamp(...), 'test_2')
]
我要做的是:
temp_array = df.to_numpy()
for i, (cols) in enumerate(temp_array):
for val in cols:
if type(val) == pd.Timestamp:
val = np.datetime64(val)
tuples = [tuple(x) for x in temp_array]
如何直接更改此元組串列中的 Timestamp 型別?更準確地說,如果型別是 pd.Timestamp,我想將其更改為 np.datetime64。
uj5u.com熱心網友回復:
這是一個應該解釋您所缺少的示例的示例。
my_list = [
['12', 123, 'test_1'],
['14', 456, 'test_2']
]
print(my_list)
for i, cols in enumerate(my_list):
for j, val in enumerate(cols):
if type(val) == int:
my_list[i][j] = str(val)
print(my_list)
結果:
[['12', 123, 'test_1'], ['14', 456, 'test_2']]
[['12', '123', 'test_1'], ['14', '456', 'test_2']]
所以完整的答案是:不要更改元組并使用索引來更改串列的內容。
但我會創建一個新串列,而不是更改現有串列。像這樣的東西:
my_list = [
['12', 123, 'test_1'],
['14', 456, 'test_2']
]
print(my_list)
new_list = [[str(item) if type(item) == int else item for item in cols] for cols in my_list]
print(new_list)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516071.html
標籤:Python熊猫麻木的
