简体   繁体   English

如何在seaborn中加载3D numpy数组作为数据框

[英]How to load 3D numpy array as dataframe in seaborn

I have data in a CSV file, for example例如,我在 CSV 文件中有数据

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.我使用 `np.genfromtxt 加载它,然后将其重塑为形状为 (3,2,3) 的 3D 数组,然后在轴 1 上求和。

I want to load it as a dataframe into Seaborn to plot it, what is the most pythonic way of doing it ?我想将它作为数据帧加载到 Seaborn 中以绘制它,最pythonic 的方法是什么?

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.我也想让它显示平均值、中位数和四分位数。

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()

在此处输入图片说明

# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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