例如,我在CSV文件中有数据

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


我使用`np.genfromtxt加载它,然后将其整形为形状为(3,2,3)的3D数组,然后在轴1上求和。

我想将其作为数据帧加载到Seaborn中以进行绘制,最Python的方式是什么?

例:

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)


我也想让它显示平均值,中位数和四分位数。

最佳答案

你可以这样使用

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


python - 如何在Seaborn中将3D numpy数组作为数据框加载-LMLPHP

# 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

关于python - 如何在Seaborn中将3D numpy数组作为数据框加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53209823/

10-12 16:57
查看更多