简体   繁体   中英

Seaborn Pairplot with Dataframe vs CSV

I have a dataframe in a Jupyter notebook and do a pairplot on it to get a bunch of plots against each other.

import seaborn as sns
sns.pairplot(df_merge)

Here is the pairplot as a result.

在此处输入图片说明

However, it plots the data incorrectly and in a non-aesthetic way. However, when I export this dataframe to a csv and then read it back into the program as a dataframe:

import seaborn as sns
df_merge.to_csv('dataframe.csv')
x = pd.read_csv('dataframe.csv')
sns.pairplot(x)

Sns plots it fine and the correlations between variables can be seen but I have an unnecessary column called Unnamed which I don't need.

在此处输入图片说明

Does anyone know what could cause this issue and how I can go about correcting it without needing to export the dataframe as a csv?

When you do:

df_merge.to_csv('dataframe.csv')

you write also the index of df_merge without a name. Then

x = pd.read_csv('dataframe.csv')

reads the index as Unnamed 0 column. To fix this, either save the data frame without index:

df_merge.to_csv('dataframe.csv', index=False)
x = pd.read_csv('dataframe.csv')

or read the csv with index:

df_merge.to_csv('dataframe.csv')
x = pd.read_csv('dataframe.csv', index_col=[0])

Figured out that the issue I was having was when I was changing the dataframe to a CSV and then changing it back to a dataframe, the values in the dataframe had a float64 type where as in my dataframe before they were all objects. Converting all the numerical columns to float before plotting the graph solved my issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM