Home > Net >  Cannot plot or use .tolist() on pd dataframe column
Cannot plot or use .tolist() on pd dataframe column

Time:09-07

so I am reading in data from a csv and saving it to a dataframe so I can use the columns. Here is my code:

filename = open(r"C:\Users\avalcarcel\Downloads\Data INSTR 9 8_16_2022 11_02_42.csv")
columns = ["date","time","ch104","alarm104","ch114","alarm114","ch115","alarm115","ch116","alarm116","ch117","alarm117","ch118","alarm118"]
df = pd.read_csv(filename,sep='[, ]',encoding='UTF-16 LE',names=columns,header=15,on_bad_lines='skip',engine='python')

length_ = len(df.date)
scan = list(range(1,length_ 1))

plt.plot(scan,df.ch104)
plt.show()

When I try to plot scan vs. df.ch104, I get the following exception thrown:

'value' must be an instance of str or bytes, not a None

So what I thought to do was make each column in my df a list:

ch104 = df.ch104.tolist()

But it is turning my data from this to this: before .tolist()

To this: after .tolist()

This also happens when I use df.ch104.values.tolist()

Can anyone help me? I haven't used python/pandas in a while and I am just trying to get the data read in first. Thanks!

CodePudding user response:

So, the df.ch104.values.tolist() code beasicly turns your column into a 2d 1XN array. But what you want is a 1D array of size N.

So transpose it before you call .tolist(). Lastly call [0] to convert Nx1 array to N array

df.ch104.values.tolist()[0]

Might I also suggest you include dropna() to avoid 'value' must be an instance of str or bytes, not a Non

df.dropna(subset=['ch104']).ch104.values.tolist()[0]

CodePudding user response:

The error clearly says there are None or NaN values in your dataframe. You need to check for None and deal with them - replace with a suitable value or delete them.

  • Related