Home > Blockchain >  creating features for DataFrame from text file in pandas
creating features for DataFrame from text file in pandas

Time:10-14

Im looking to create features for a dataframe using feature_names.txt using the following code :

import pandas as pd
features = pd.read_csv("feature_names.txt", header= None)
dataset = pd.read_csv("dataset.csv")
dataset.columns = features.values

this outputs the following :

(id ,) (age,) (workclass,) (fnlwgt,) (education,) (education-num,) (marital-status,) (occupation,) (relationship,)

Im looking to remove the brackets and comma but have using the following code:

dataset.columns.str.replace("[()]", "")

this just makes all the headers nan

I would appreciate any help, Thanks very much.

CodePudding user response:

If 'features' is a Series, you could try:

dataset.columns = features

If it's a dataframe, then:

dataset.columns = features['columnName']

Or if you want the index values as column names:

dataset.columns = features.index
  • Related