Index(['a','b','c'],dtype='object')a b c 同时索引也有它的方法和属性。 查看DataFrame的常用属性 包含values、index、columns、ndim和shape。 Pandas索引操作 1.重建索引
data.index # 结果 Index(['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9'], dtype='object') (3)columns DataFrame的列索引列表 data.columns # 结果 Index(['语文', '数学', '英语', '政治', '体育'], dtype='object') (4)va...
In [31]: df[["foo", "qux"]].columns.to_numpy() Out[31]: array([('foo', 'one'), ('foo', 'two'), ('qux', 'one'), ('qux', 'two')], dtype=object) # for a specific level In [32]: df[["foo", "qux"]].columns.get_level_values(0) Out[32]: Index(['foo', 'f...
astype('category') >>> df['年级'] 0 高二 1 高三 2 高二 3 高三 4 高一 5 高一 6 高二 7 高三 Name: 年级, dtype: category Categories (3, object): ['高一', '高三', '高二'] 通过cat.set_categories()更新类别数据同时添加缺少的类别。 >>> df['年级'] = df['年级'].cat.set_...
这里提到了index和columns分别代表行标签和列标签,就不得不提到pandas中的另一个数据结构:Index,例如series中标签列、dataframe中行标签和列标签均属于这种数据结构。既然是数据结构,就必然有数据类型dtype属性,例如数值型、字符串型或时间类型等,其类型绝大多数场合并不是我们关注的主体,但有些时候值得注意,如后文中...
Name: 分类, dtype: int64 #两种绘图方式均可 #cg.plot(kind='bar',rot=45) cg.plot.bar(rot=45) plt.xlabel('分类') plt.ylabel('频数') plt.title('新闻分类分布条形图') 2、DataFrame.plot.bar() DataFrame绘制条形图时,会将每一行的值分为一组,各列名称作为图例 ...
In [10]: ser_ad = pd.Series(data, dtype=pd.ArrowDtype(pa.string())) In [11]: ser_ad.dtype == ser_sd.dtype Out[11]:FalseIn [12]: ser_sd.str.contains("a") Out[12]:0True1False2Falsedtype: boolean In [13]: ser_ad.str.contains("a") ...
In [7]: df.info(memory_usage="deep") <class 'pandas.core.frame.DataFrame'> RangeIndex: 5000 entries, 0 to 4999 Data columns (total 8 columns): # Column Non-Null Count Dtype --- --- --- --- 0 int64 5000 non-null int64 1 float64 5000 non-null float64 2 datetime64[ns] 5000...
pd.read_excel("path_to_file.xls", dtype={"MyInts": "int64", "MyText": str})```### 写入 Excel 文件### 将 Excel 文件写入磁盘要将 `DataFrame` 对象写入 Excel 文件的一个工作表中,可以使用 `to_excel` 实例方法。参数与上面描述的 `to_csv` 大致相同,第一个参数是 Excel 文件的名称,可选...
# Get the information of the dataframe print(df.info()) # Output: # RangeIndex: 35 entries, 0 to 34 # Data columns (total 5 columns): # Column Non-Null Count Dtype # --- --- --- --- # 0 id 35 non-null int64 # 1 name 35 non-null object # 2 class 35 non-null object ...