Search code examples
pythonpandasspyder

Extracting specific columns from pandas.dataframe


I'm trying to use python to read my csv file extract specific columns to a pandas.dataframe and show that dataframe. However, I don't see the data frame, I receive Series([], dtype: object) as an output. Below is the code that I'm working with: My document consists of: product sub_product issue sub_issue consumer_complaint_narrative
company_public_response company state zipcode tags
consumer_consent_provided submitted_via date_sent_to_company
company_response_to_consumer timely_response consumer_disputed?
complaint_id

I want to extract : sub_product issue sub_issue consumer_complaint_narrative

import pandas as pd

df=pd.read_csv("C:\\....\\consumer_complaints.csv")
df=df.stack(level=0)
df2 = df.filter(regex='[B-F]')
df[df2]

Solution

  • import pandas as pd
    
    input_file = "C:\\....\\consumer_complaints.csv"
    dataset = pd.read_csv(input_file)
    df = pd.DataFrame(dataset)
    cols = [1,2,3,4]
    df = df[df.columns[cols]]
    

    Here specify your column numbers which you want to select. In dataframe, column start from index = 0

    cols = []
    

    You can select column by name wise also. Just use following line

    df = df[["Column Name","Column Name2"]]