給定一個系列和一列的(唯一)dtype,我希望里面的 dtype 資訊作為一個字串。
import numpy as np
import pandas as pd
df = pd.DataFrame(columns = ['col1', 'col2'], data = [[1,2], [3,4]])
df.dtypes[0]
上面代碼的輸出

目標:'int64'從 中提取字串df.dtypes[0],即dtype('int64').
沒有成功在網上找到解決方案。
uj5u.com熱心網友回復:
只需轉換為字串:
df.dtypes.astype(str)[0]
或者,如果您真的只對單個值感興趣,請使用該name屬性。
df.dtypes[0].name
輸出: 'int64'
對于整個系列:
>>> df.dtypes.astype(str)
col1 int64
col2 int64
dtype: object
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408941.html
標籤:
