Python Pandas – 获取数据类型和 DataFrame 列信息
要获取数据类型和DataFrame列信息,请使用该info()方法。使用别名导入所需的库-
import pandas as pd;
创建一个包含3列的DataFrame-
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80] } )
获取有关DataFrame的数据类型和其他信息-
dataFrame.info()
示例
以下是代码-
import pandas as pd; #创建一个数据框 dataFrame = pd.DataFrame( { "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80] } ) print"DataFrame ...\n",dataFrame #从列中获取唯一值 print"\nUnique values from a column ...\n",dataFrame['Car'].unique() print"\nCount unique values from a column ...\n",dataFrame['Car'].nunique() #获取数据类型信息 print"\n Get the datatype info ...\n",dataFrame.info()输出结果
这将产生以下输出-
DataFrame ... Car Place Units 0 BMW Delhi 100 1 Audi Bangalore 150 2 BMW Hyderabad 50 3 Lexus Chandigarh 110 4 Tesla Pune 90 5 Lexus Mumbai 120 6 Mustang Jaipur 80 Unique values from a column ... ['BMW' 'Audi' 'Lexus' 'Tesla' 'Mustang'] Count unique values from a column ... 5 Get the datatype info ... RangeIndex: 7 entries, 0 to 6 Data columns (total 3 columns): Car 7 non-null object Place 7 non-null object Units 7 non-null int64 dtypes: int64(1), object(2) memory usage: 240.0+ bytes None