pandas 从数组python创建一个数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53961914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:14:18  来源:igfitidea点击:

Create a dataframe from arrays python

pythonpandasdataframe

提问by theantomc

I'm try to construct a dataframe (I'm using Pandas library) from some arrays and one matrix.

我试图从一些数组和一个矩阵构建一个数据框(我正在使用 Pandas 库)。

in particular, if I have two array like this:

特别是,如果我有两个这样的数组:

A=[A,B,C]
B=[D,E,F]

And one matrix like this :

一个像这样的矩阵:

1 2 2
3 3 3
4 4 4

Can i create a dataset like this?

我可以创建这样的数据集吗?

  A B C
D 1 2 2
E 3 3 3
F 4 4 4

Maybe is a stupid question, but i m very new with Python and Pandas.

也许是一个愚蠢的问题,但我对 Python 和 Pandas 很陌生。

I seen this :

我看到了这个:

https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.html

https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.html

but specify only 'colums'.

但仅指定“列”。

I should read the matrix row for row and paste in my dataset, but I m think that exist a more easy solution with Pandas.

我应该读取行的矩阵行并粘贴到我的数据集中,但我认为 Pandas 存在一个更简单的解决方案。

回答by gorjan

This should do the trick for you.

这应该对你有用。

columns = ["A", "B", "C"]
rows = ["D", "E", "F"]
data = np.array([[1, 2, 2], [3, 3, 3],[4, 4, 4]])
df = pd.DataFrame(data=data, index=rows, columns=columns)

回答by Rodwan Bakkar

You can do like this:

你可以这样做:

a=[[1, 2, 2],[1, 2, 2],[1, 2, 2]]
df=pd.DataFrame(a)
df.columns = ['a', 'b', 'c']
df.index = ['d', 'e', 'f']
print(df)

回答by Windchill

is this what you need?

这是你需要的吗?

import pandas as pd
A=['A','B','C']
B=['D','E','F']
C=[[1,2,2],[3,3,3],[4,4,4]]

df=pd.DataFrame(C, columns=A)
df.index=B
df.head()

    A   B   C
D   1   2   2
E   3   3   3
F   4   4   4