Sumit Vaz
Sumit Vaz

Reputation: 5

I tried adding column names to a dataframe and it deleted all data i added to it

This is the code I used. I first read the data to variable 'dbs' and then converted it into a DataFrame named 'dtabse' with column names 'Username' and 'Password'.

dbs = pd.read_csv('pass_users.txt', header=None)

dtabse= pd.DataFrame(dbs, columns=['Username', 'Password'])

But upon running it, all my data turned into NaN

The image linked below shows the output I'm getting

Any help will be appreicated. Thank you!

Upvotes: 0

Views: 47

Answers (2)

Camilo Martínez M.
Camilo Martínez M.

Reputation: 1620

Your dbs = pd.read_csv('pass_users.txt', header=None) already gives you a dataframe, so your second line isn't necessary. What you'd need to do to change the column names would be

dbs.columns = ['Username', 'Password']

In fact, I think you can do this immediately

dbs = pd.read_csv('pass_users.txt', names=['Username', 'Password'])

Upvotes: 4

Joep
Joep

Reputation: 832

In addition to Camilo's answer, if you have multiple columns and only want to keep a selection:

dbs = pd.read_csv('pass_users.txt', header=None)
dbs = dbs[['Username', 'Password']]   # Enter columns to keep

Upvotes: 0

Related Questions