新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
pandas判斷列是否存在
在 Pandas 中,可以使用 in 運算符來判斷列是否存在于 DataFrame 中,下面是一個詳細(xì)的示例:

成都創(chuàng)新互聯(lián)是一家專業(yè)提供凌海企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站設(shè)計、H5技術(shù)、小程序制作等業(yè)務(wù)。10年已為凌海眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。
判斷列是否存在
要判斷一個列是否存在于 DataFrame 中,可以使用 in 運算符結(jié)合 columns 屬性來實現(xiàn),下面是具體的步驟:
1、導(dǎo)入 Pandas 庫:
import pandas as pd
2、創(chuàng)建一個 DataFrame:
data = {'Name': ['John', 'Emma', 'Mike'],
'Age': [25, 28, 30],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
3、使用 in 運算符和 columns 屬性判斷列是否存在:
column_name = 'Age'
if column_name in df.columns:
print(f"The column '{column_name}' exists in the DataFrame.")
else:
print(f"The column '{column_name}' does not exist in the DataFrame.")
運行以上代碼,將輸出以下結(jié)果:
The column 'Age' exists in the DataFrame.
如果將 column_name 的值改為不存在的列名,'Gender',則輸出結(jié)果為:
The column 'Gender' does not exist in the DataFrame.
通過這種方式,你可以方便地判斷列是否存在于 DataFrame 中。
文章名稱:pandas判斷列是否存在
網(wǎng)頁URL:http://www.dlmjj.cn/article/djgphgg.html


咨詢
建站咨詢
