在CSV文件中选择某些列

在CSV文件中选择某些列

本文介绍了如何使用 pandas 在CSV文件中选择某些列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是为了开始编码之旅而已,并在youtube上观看了许多教程,现在我正尝试使用jupyter将数据集从SPSS导入"到python中.

I only just started my coding journey in order to and have watched a bunch of tutorials on youtube and am now trying to 'import' a dataset from SPSS into python using jupyter.

到目前为止,我已经设法将.sav转换为.csv文件,并使用以下代码进行读取.我想选择数据中的某些列并将它们存储在新的csv文件中,以便对它们进行一些分析,并尝试构建一个脚本来预测某些事物和特征.问题是我有数百个数据列,并且只希望从3或4开始.

So far I've managed to convert the .sav into a .csv file and read it using the code below. I want to select certain columns in my data and store them in a new csv file in order to do some analysis on them and try to build a script to predict certain things and characteristics. Problem is i have hundreds of data columns and only want 3 or 4 to start with.

我尝试使用data.drop()函数,但很快意识到必须有更好的方法来做到这一点?

i tried using the data.drop() function but soon realized there must be a better way to do this?

由于这是我在这里的第一篇文章,因此我无法以更好的方式对此进行解释,我深表歉意.

I apologize in advance for my inability to explain this in a better way as this is my very first post here.

import pandas as pd
df = pd.read_csv('csvfile.csv')
df

推荐答案

使用此:

import pandas as pd
df = pd.read_csv('csvfile.csv' , usecols = ['col1','col2'])
df

在"col1"和"col2"的位置输入列名.然后将它们写入另一个csv中,请执行以下操作:

Inplace of 'col1' and 'col2' enter the column names. Then to write them into another csv , do this:

df.to_csv('csv_file_1.csv' , index = False)

这篇关于如何使用 pandas 在CSV文件中选择某些列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:02