Python Pandas 代码解析:读取 CSV 文件并提取特定列
Python Pandas 代码解析:读取 CSV 文件并提取特定列
xigua40 = pd.read_csv('example.csv', index_col=1, encoding='gbk')
x = xigua40[['密度', '含糖量']].values
这段代码使用了 Python 的 Pandas 库,可以实现读取 CSV 文件并提取特定列数据为 NumPy 数组的功能。
-
xigua40 = pd.read_csv('example.csv', index_col=1, encoding='gbk')pd.read_csv('example.csv'):使用 Pandas 的read_csv函数读取名为 'example.csv' 的 CSV 文件。index_col=1:指定 CSV 文件的第二列作为结果 DataFrame 的索引列。encoding='gbk':指定 CSV 文件的编码格式为 GBK。
-
xigua40[['密度', '含糖量']]xigua40[['密度', '含糖量']]:使用双方括号选择 DataFrame 'xigua40' 中的 '密度' 和 '含糖量' 两列,并创建一个新的 DataFrame。
-
.values.values:从选定的 DataFrame 中提取数据,并以 NumPy 数组的形式返回。最终的数组包含两列,分别对应 '密度' 和 '含糖量'。
这段代码可以帮助您从 CSV 文件中读取数据并进行数据分析。
原文地址: https://www.cveoy.top/t/topic/otRt 著作权归作者所有。请勿转载和采集!