pandas 从多个列表创建一个熊猫数据框

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

create a pandas data frame from several lists

pythonfor-looppandasdataframedata-manipulation

提问by user3495042

My function outputs a list, for instance when I type:

我的函数输出一个列表,例如当我输入:

My_function('TV', 'TV_Screen')

it outputs the following:

它输出以下内容:

['TV', 1, 'TV_Screen', 0.04, 'True']

Now, my TV is made of several parts, such as speaker, transformer, etc., I can keep running my function for each part, and for instance change 'TV_Screen' for 'TV_Speaker', or 'TV_transformer', etc.

现在,我的电视由几个部分组成,例如扬声器、变压器等,我可以继续为每个部分运行我的功能,例如将“TV_Screen”更改为“TV_Speaker”或“TV_transformer”等。

The alternative is to create a list with all the part, such as:

另一种方法是创建一个包含所有零件的列表,例如:

TV_parts = ['TV_Screen', 'TV_Speaker', 'TV_transformer']

What I am trying to get is a pandas data frame with 5 columns (because my function outputs 5 variables, see above the section "it outputs the following:") and in this case 3 rows (one of each for 'TV_Screen', 'TV_Speaker', and 'TV_transformer'). Basically, I want the following to be in a data frame:

我想要得到的是一个有 5 列的 Pandas 数据框(因为我的函数输出 5 个变量,请参阅上面的“它输出以下内容:”部分),在这种情况下,有 3 行(“TV_Screen”中的每一行,“ TV_Speaker”和“TV_transformer”)。基本上,我希望以下内容在数据框中:

['TV', 1, 'TV_Screen', 0.04, 'True']
['TV', 9, 'TV_Speaker', 0.56, 'True']
['TV', 3, 'TV_transformer', 0.80, 'False']

I know I need a for loop somewhere, but I am not sure how to create this data frame. Could you please help? (I can change the output of my function to be a pd.Series or something else that would work better).

我知道我在某处需要一个 for 循环,但我不确定如何创建这个数据框。能否请你帮忙?(我可以将函数的输出更改为 pd.Series 或其他更好的东西)。

Thanks!

谢谢!

回答by andrewwowens

If you have many arrays, it may be worth converting them into a numpy matrix first and then converting them into a dataframe.

如果您有许多数组,可能值得先将它们转换为 numpy 矩阵,然后再将它们转换为数据帧。

import pandas as pd
import numpy as np

a = ['TV', 1, 'TV_Screen', 0.04, 'True']
b = ['TV', 9, 'TV_Speaker', 0.56, 'True']
c = ['TV', 3, 'TV_transformer', 0.80, 'False']

matrix = np.matrix([a,b,c])

df = pd.DataFrame(data=matrix)

回答by elyase

You can do it like this:

你可以这样做:

def My_function(part):
    # prepare result
    result = ['TV', 1, part, 0.04, 'True'] # for testing 
    return result

TV_parts = ['TV_Screen', 'TV_Speaker', 'TV_transformer']
df = pd.DataFrame([My_function(part) for part in TV_parts])

>>> df

    0  1               2     3     4
0  TV  1       TV_Screen  0.04  True
1  TV  1      TV_Speaker  0.04  True
2  TV  1  TV_transformer  0.04  True