Home > Software engineering >  Python pandas dataframe returning NaN while defining columns name
Python pandas dataframe returning NaN while defining columns name

Time:10-08

I'm new in python and I'm trying to read a csv file, delete some columns that are not usefull and write it into another csv file. I manage to do this, but I want to add columns name to my csv, i'm using dataframe to do this but the values are returning NaN while i'm having real values in my tab.

import pandas as pd
df = pd.read_csv('testData.csv')
x = df.drop(columns=['2','3','7','8','9','10','11'])
y = pd.DataFrame(x, columns=['TimeStamp','OpenTemp','CloseTemp','MeanTemp','EndTimeStamp'])
y

This is my input Input

And this is my output I have now Output

I'd like to have my values instead of NaN

Does anyone knows what am i doing wrong?

CodePudding user response:

If you want to rename your columns I would recommend setting them in the existing DataFrame, and not creating a new one.

x.columns = ['TimeStamp','OpenTemp','CloseTemp','MeanTemp','EndTimeStamp']

CodePudding user response:

Use read_csv differently:

df = pd.read_csv('testData.csv', header=0,
                 usecols=[0, 1, 4, 5, 6],
                 names=['TimeStamp','OpenTemp','CloseTemp','MeanTemp','EndTimeStamp'])

If your csv contains header and you don't want to use it, set header=0 and define the column names (names=[...]). Select the subset columns you want to keep using usecols=[...].

  • Related