0

Here's the dataset :

1.1;1.2;1.3;2.1;2.2;2.3;2.4;3.1;3.2;3.3;3.4;4.1;4.2;4.3;5.1;5.2
1;1;-;1;1;1;-;1;1;2;-;-;1;-;-;-
6;6;5;6;6;6;6;4;3;2;3;6;0;5;5;2
2;1;1;1;1;2;1;1;1;1;1;1;1;1;1;1
4;1;2;4;1;2;-;2;1;-;1;-;-;1;-;-
1;1;-;1;-;1;-;-;1;-;-;-;-;-;-;-
0;0;0;1;2;0;0;0;0;1;1;0;0;0;0;0
1;1;1;1;0;1;1;1;1;2;1;1;-;-;-;-
0;1;1;1;-;0;-;-;0;-;-;-;-;-;-;-
2;0;-;0;1;-;-;0;1;-;1;-;-;-;-;-
-;1;1;1;-;1;-;-;1;-;-;-;-;1;-;-
2;1;1;1;1;1;1;1;1;-;1;1;-;1;-;-
0;2;-;4;1;1;-;1;0;-;0;-;-;0;-;-
0;-;-;1;1;2;1;-;-;-;-;-;-;-;1;-
1;1;2;1;1;1;1;1;1;2;1;1;1;1;1;1
1;1;1;1;1;1;-;1;1;1;1;1;0;1;-;-
2;1;1;1;1;1;-;1;1;-;1;1;-;-;-;-
3;3;1;0;2;1;0;1;1;0;0;1;0;1;1;0
3;4;-;2;1;-;-;1;1;-;1;1;-;-;-;-
4;1;1;1;3;2;3;1;1;2;1;1;2;0;4;4

Here's the code:

import csv
import glob
import re
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns

files = glob.glob(f"session*C.csv")

# load dataset with pandas
df = pd.read_csv(files[0], sep = ';')

# replace "-" data with 0
dfs = df.replace(r'^-$', 0, regex=True)

print(dfs)

# Seaborn boxplot
sns.set_style('darkgrid')
fig, ax = plt.subplots(figsize=(9, 10))

sns.boxplot(data=dfs, orient="h", ax=ax)

plt.show()

Two weird bugs here:

  1. Boxplot only outputs column 2.1.
  2. Dataframes df and dfs exhibit a weird one-space column before column 2.1, the one that gets plotted!

The code works perfectly fine with other datasets. I searched and tested things for hours.

macxpat
  • 173
  • 2
  • 11
  • Can you show an example of expected output (even if it doesn't match the sample data shared here)? Just to have an idea. – lemon Jun 11 '22 at 16:23
  • 2
    Add `dfs = dfs.astype(int)` after `dfs = df.replace(r'^-$', 0, regex=True)`, because all the other columns are strings (which contained `'-'`). – Trenton McKinney Jun 11 '22 at 16:44
  • @Trenton Thanks! this solves the issue. Still a weird behavior from pandas. Why replacing `-` with `0` and not `"0"` doesn't create integer data? Why some columns get plotted and others no? – macxpat Jun 12 '22 at 01:18
  • it is not weird. It works with columns, not single cells. If in column it sees string which it can't convert to number - i.e. `-` - then it doesn't convert this column to integers. So you replace `-` with integer `0` but other items in this column are still strings. – furas Jun 12 '22 at 13:32

0 Answers0