在Python中,输出两列数据可以通过多种方式实现,以下是使用Pandas库和NumPy库的两种常见方法:
使用Pandas库
import pandas as pd
创建一个简单的数据字典
data = {
'name': ['Alice', 'Bob', 'Charlie', 'David'],
'age': [25, 30, 35, 40]
}
将字典转换为Pandas DataFrame
df = pd.DataFrame(data)
以表格形式输出两列数据
print(df.to_string(index=False, header=True))
使用NumPy库
import numpy as np
创建一个简单的NumPy数组
x = np.array([[1, 2, 5], [2, 3, 5], [3, 4, 5], [2, 3, 6]])
输出数组的形状,即行数和列数
print(x.shape) 输出:(4, 3)
输出数组的行数和列数
print(x.shape) 输出:4
print(x.shape) 输出:3
以上代码示例展示了如何使用Pandas和NumPy库输出两列数据。Pandas库适合处理结构化数据,而NumPy库适合处理数值计算和矩阵操作。