将列表读入 Pandas DataFrame 的列中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29014618/
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-13 23:02:31  来源:igfitidea点击:

Read lists into columns of pandas DataFrame

pythonlistpandas

提问by Steven C. Howell

I want to load lists into columns of a pandas DataFrame but cannot seem to do this simply. This is an example of what I want using transpose()but I would think that is unnecessary:

我想将列表加载到 Pandas DataFrame 的列中,但似乎不能简单地做到这一点。这是我想要使用的示例,transpose()但我认为这是不必要的:

In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: x = np.linspace(0,np.pi,10)
In [4]: y = np.sin(x)
In [5]: data = pd.DataFrame(data=[x,y]).transpose()
In [6]: data.columns = ['x', 'sin(x)']
In [7]: data
Out[7]: 
          x        sin(x)
0  0.000000  0.000000e+00
1  0.349066  3.420201e-01
2  0.698132  6.427876e-01
3  1.047198  8.660254e-01
4  1.396263  9.848078e-01
5  1.745329  9.848078e-01
6  2.094395  8.660254e-01
7  2.443461  6.427876e-01
8  2.792527  3.420201e-01
9  3.141593  1.224647e-16

[10 rows x 2 columns]

Is there a way to directly load each list into a column to eliminate the transpose and insert the column labels when creating the DataFrame?

有没有办法在创建DataFrame时直接将每个列表加载到列中以消除转置并插入列标签?

回答by Steven C. Howell

Someone just recommended creating a dictionary from the data then loading that into the DataFrame like this:

有人只是建议从数据创建一个字典,然后像这样将其加载到 DataFrame 中:

In [8]: data = pd.DataFrame({'x': x, 'sin(x)': y})
In [9]: data
Out[9]: 
          x        sin(x)
0  0.000000  0.000000e+00
1  0.349066  3.420201e-01
2  0.698132  6.427876e-01
3  1.047198  8.660254e-01
4  1.396263  9.848078e-01
5  1.745329  9.848078e-01
6  2.094395  8.660254e-01
7  2.443461  6.427876e-01
8  2.792527  3.420201e-01
9  3.141593  1.224647e-16

[10 rows x 2 columns]

Note than a dictionary is an unordered set of key-value pairs. If you care about the column orders, you should pass a list of the ordered key values to be used (you can also use this list to only include some of the dict entries):

请注意,字典是一组无序的键值对。如果您关心列顺序,则应该传递要使用的有序键值列表(您也可以使用此列表仅包含一些 dict 条目):

data = pd.DataFrame({'x': x, 'sin(x)': y}, columns=['x', 'sin(x)'])

回答by dabru

Here's another 1-line solution preserving the specified order, without typing xand sin(x)twice:

这是另一个保留指定顺序的 1-line 解决方案,无需键入xsin(x)两次:

data = pd.concat([pd.Series(x,name='x'),pd.Series(y,name='sin(x)')], axis=1)