python中columns是什么意思_Python Pandas DataFrame.columns用法及代码示例

Pandas DataFrame是带有标签轴(行和列)的二维大小可变的,可能是异构的表格数据结构。算术运算在行和列标签上对齐。可以将其视为Series对象的dict-like容器。这是 Pandas 的主要数据结构。

Pandas DataFrame.columns属性返回给定Dataframe的列标签。

用法: DataFrame.columns

参数:没有

返回:列名

范例1:采用DataFrame.columns属性以返回给定Dataframe的列标签。

# importing pandas as pd

import pandas as pd

# Creating the DataFrame

df = pd.DataFrame({'Weight':[45, 88, 56, 15, 71],

'Name':['Sam', 'Andrea', 'Alex', 'Robin', 'Kia'],

'Age':[14, 25, 55, 8, 21]})

# Create the index

index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5']

# Set the index

df.index = index_

# Print the DataFrame

print(df)

输出:

现在我们将使用DataFrame.columns属性以返回给定 DataFrame 的列标签。

# return the column labels

result = df.columns

# Print the result

print(result)

输出:

正如我们在输出中看到的,DataFrame.columns属性已成功返回给定 DataFrame 的所有列标签。

范例2:采用DataFrame.columns属性以返回给定Dataframe的列标签。

# importing pandas as pd

import pandas as pd

# Creating the DataFrame

df = pd.DataFrame({"A":[12, 4, 5, None, 1],

"B":[7, 2, 54, 3, None],

"C":[20, 16, 11, 3, 8],

"D":[14, 3, None, 2, 6]})

# Create the index

index_ = ['Row_1', 'Row_2', 'Row_3', 'Row_4', 'Row_5']

# Set the index

df.index = index_

# Print the DataFrame

print(df)

输出:

现在我们将使用DataFrame.columns属性以返回给定 DataFrame 的列标签。

# return the column labels

result = df.columns

# Print the result

print(result)

输出:

正如我们在输出中看到的,DataFrame.columns属性已成功返回给定 DataFrame 的所有列标签。

  • 8
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值