簡體   English   中英

如何在 Python 中打印僅變量 dtype 的對象

[英]How to print variable dtype only object in Python

我對使用 Python 很陌生。 我想打印 'object' dtype 的變量。 你能幫我語法嗎? 我試過下面的代碼

df.info()

但它給出了所有帶有 dtypes float 和 int 變量的列表。 我只想打印對象 dtype 變量。

例如:當我使用df.info()我得到了以下結果。 但我不想要除 object 之外的其他 dtypes。

RangeIndex: 1460 entries, 0 to 1459
Data columns (total 81 columns):
Id               1460 non-null int64
MS               1460 non-null int64
MSZ              1460 non-null object
Lo               1201 non-null float64
LA               1460 non-null int64
St               1460 non-null object
Al               91 non-null object ```

我想從數據框中打印僅是對象 dtype 的變量

df = pd.DataFrame({'Id': [1], 'Name': ['xyz'], 'Weight': [12.34], 'Date': pd.to_datetime('2020-01-01'), '?': None})

df.select_dtypes('O').columns.to_list()
#['Name', '?']

或者,如果您想每行打印一個名稱:

print(*df.select_dtypes('O').columns.to_list(), sep='\n')
#Name
#?

暫無
暫無

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

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