如何获取 Pandas DataFrame 的第一列?
在本文中,我们将讨论如何在Python编程语言中获取 pandas 数据框的第一列。
方法一:使用 iloc[]函数
此函数用于使用切片运算符获取第一列。对于我们提取所有行的行,对于列指定第一列的索引。
语法:
dataframe.iloc[:, 0]
在哪里。数据框是输入数据框
切片操作如下:
[row_start:row_end , column_start, column_end]
where,
- row_start refers start row with 0 position as index
- row_end refers last row with n th position as index
- column_start refers start column with 0 position as index
- column_end refers last column with n th position as index
也可以采用以下替代方案。
句法 :
dataframe.iloc[:, :1]
在哪里。数据框是输入数据框。这两个都将返回数据帧数据类型。
示例:使用上述方法获取第一列的Python程序
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# get first column by returning series
print(data.iloc[:, 0])
print("--------------")
# get first column by returning dataframe
print(data.iloc[:, :1])
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# get first column by returning dataframe
# using columns[] method
print(data[data.columns[0]])
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# get first column by returning dataframe
# using column_name
print(data.id)
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# get first column by returning dataframe
# using column_name
# display 1 row
print(data.id.head(1))
print("--------------")
# get first column by returning dataframe
# using column_name
# display 2 rows
print(data.id.head(2))
print("--------------")
# get first column by returning dataframe
# using column_name
# display 4 rows
print(data.id.head(4))
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# using head
print(data.T.head(1).T)
输出:
方法2:使用列[]
此方法将根据索引返回列。所以,我们必须给 0 才能得到第一列
语法:
dataframe[dataframe.columns[0]]
在哪里
- 数据框是输入数据框
- columns[0] 代表第一列
示例:使用 columns[] 获取第一列的Python程序
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# get first column by returning dataframe
# using columns[] method
print(data[data.columns[0]])
输出:
方法 3:使用列名
我们可以使用第一列名称来获取第一列。
语法:
dataframe.first_column
示例:使用列名获取第一列的Python代码
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# get first column by returning dataframe
# using column_name
print(data.id)
输出:
我们还可以使用其中的 head()函数来显示第一列中的行数。
示例:使用列名获取第一列的Python代码
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# get first column by returning dataframe
# using column_name
# display 1 row
print(data.id.head(1))
print("--------------")
# get first column by returning dataframe
# using column_name
# display 2 rows
print(data.id.head(2))
print("--------------")
# get first column by returning dataframe
# using column_name
# display 4 rows
print(data.id.head(4))
输出:
方法四:使用 head()函数
默认情况下,此函数返回数据框的顶部行。要返回该列,我们必须使用 T函数转置(将行转换为列)数据帧并获取第一列。
语法:
dataframe.T.head(1).T
示例:使用列名获取第一列的Python代码
Python3
# import pandas module
import pandas as pd
# create dataframe with 3 columns
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
# display dataframe
print(data)
print("--------------")
# using head
print(data.T.head(1).T)
输出: