簡體   English   中英

Pandas:獲取列 dtype 作為字符串

[英]Pandas: Get column dtype as string

給定一個系列和一列的(唯一)dtype,我希望里面的 dtype 信息作為一個字符串。

import numpy as np
import pandas as pd

df = pd.DataFrame(columns = ['col1', 'col2'], data = [[1,2], [3,4]])
df.dtypes[0]

上面代碼的output

上面代碼的示例輸出

目標:從df.dtypes[0]中提取字符串'int64' ,即 dtype dtype('int64')

沒有成功在網上找到解決方案。

只需轉換為字符串:

df.dtypes.astype(str)[0]

或者,如果您真的只對單個值感興趣,請使用name屬性。

df.dtypes[0].name

Output: 'int64'

對於整個系列:

>>> df.dtypes.astype(str)
col1    int64
col2    int64
dtype: object

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM