How to delete a column from a data frame with pandas?

newWithPython :

I read my data

import pandas as pd
df = pd.read_csv('/path/file.tsv', header=0, delimiter='\t')
print df

and get:

          id    text
0    361.273    text1...
1    374.350    text2...
2    374.350    text3...

How can I delete the id column from the above data frame?. I tried the following:

import pandas as pd
df = pd.read_csv('/path/file.tsv', header=0, delimiter='\t')
print df.drop('id', 1)

But it raises this exception:

ValueError: labels ['id'] not contained in axis
EdChum :

To actually delete the column

del df['id'] or df.drop('id', 1) should have worked if the passed column matches exactly

However, if you don't need to delete the column then you can just select the column of interest like so:

In [54]:

df['text']
Out[54]:
0    text1
1    text2
2    textn
Name: text, dtype: object

If you never wanted it in the first place then you pass a list of cols to read_csv as a param usecols:

In [53]:
import io
temp="""id    text
363.327    text1
366.356    text2
37782    textn"""
df = pd.read_csv(io.StringIO(temp), delimiter='\s+', usecols=['text'])
df
Out[53]:
    text
0  text1
1  text2
2  textn

Regarding your error it's because 'id' is not in your columns or that it's spelt differently or has whitespace. To check this look at the output from print(df.columns.tolist()) this will output a list of the columns and will show if you have any leading/trailing whitespace.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to delete a column from a data frame with pandas?

How to create a new rows from column values of pandas data frame

How to match multiple words from list with pandas data frame column

How to create a dummy column from a string expression in pandas data frame?

How to strip symbol from pandas data frame column

In Pandas, how to delete rows from a Data Frame based on another Data Frame?

How to update the data frame column values from another data frame based a conditional match in pandas

Pandas: Selecting column from data frame

How to delete duplicated clients based on value in datetime column in Data Frame in Python Pandas?

How to spread a column in a Pandas data frame

How to create a new column in a pandas data frame

how to clip pandas for a multiple column in a data frame

How to delete certain condition from data frame

How to remove/delete first row/column from Data Frame using python?

How is it possible that updating a numpy array derived from a Pandas DatFrame column also (unexpectedly) updates the data frame column?

How can I create a new column in a pandas data frame by extracting words from sentences in another column?

How to extract information from one column to create a new column in a pandas data frame

How to create new pandas columns from a pandas data frame column content

How to delete fraction of rows that has specific attribute value from pandas data frame

Delete a string phrase from a data frame column and replace it python

Plot a column from a data frame as a function of another column in pandas

How to delete the last column of data of a pandas dataframe

How can I normalize a nested JSON object from a pandas column and append to existing data frame or as its own data frame?

How to insert a column in a specific place in Pandas data frame? (Change column order in pandas data frame)

Pandas: New column from conditions and from another data frame

How to delete column from pandas DataFrame?

R: How to delete rows of a data frame based on the values of a given column

How to create new pandas column based on other column of data frame?

How to make a column in data frame as a function of two others column in pandas?