1

I have data in a CSV file, for example

0, 3, 4, 2, 4, 2
2, 1, 7, 2, 8, 3
3, 4, 6, 2, 5, 1

I load it using `np.genfromtxt, then reshape it into a 3D array of shape (3,2,3), then sum it on axis 1.

I want to load it as a dataframe into Seaborn to plot it, what is the most pythonic way of doing it ?

Example:

values = np.genfromtxt('data.csv', delimiter=',')    # len(values) evaluates
values = np.reshape(values,(3,2,len(values)))        # to 3 in that case      
sum_rates = np.sum(values,axis=1)
sns.lineplot(values)

I also would like to get it to display the average, the median and the quartiles.

1 Answer 1

1

You can use something like this,

import pandas as pd

filname = r'path/to/file'
df = pd.read_csv(filename, header=None)
print df
   0  1  2  3  4  5
0  0  3  4  2  4  2
1  2  1  7  2  8  3
2  3  4  6  2  5  1

# to get sum
sum = df.sum(axis=1)
print sum
0    15
1    23
2    21

# to plot data
df.plot()

enter image description here

# to get count, mean, std ...
print df.describe()
              0         1         2    3         4    5
count  3.000000  3.000000  3.000000  3.0  3.000000  3.0
mean   1.666667  2.666667  5.666667  2.0  5.666667  2.0
std    1.527525  1.527525  1.527525  0.0  2.081666  1.0
min    0.000000  1.000000  4.000000  2.0  4.000000  1.0
25%    1.000000  2.000000  5.000000  2.0  4.500000  1.5
50%    2.000000  3.000000  6.000000  2.0  5.000000  2.0
75%    2.500000  3.500000  6.500000  2.0  6.500000  2.5
max    3.000000  4.000000  7.000000  2.0  8.000000  3.0
Sign up to request clarification or add additional context in comments.

1 Comment

The issue with your answer is that the data is loaded as 2D, not 3D. I want to sum over each non-overlapping pair of each line here for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.