pandas 如何在Python中的列表开头插入元素

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

How to insert element at the beginning of list in Python

pythonpython-2.7python-3.xpandas

提问by Vishal Mishra

I have two lists and the first one is having additional data (102) when compared to second one.

我有两个列表,第一个列表与第二个列表相比有额外的数据 (102)。

First List:

第一份名单:

['102 1 1 v-csm CSM vCSM enabled active up D:042 H:20 M:14 S:58 5.4.4.4-68.REL']

Second List:

第二个名单:

['2  1 v-csm  CSM  vCSM  enabled active up  D:331 H:12 M:20 S:33  5.4.4.4-68.REL']

I want to insert a string like "xyz" at first position of the list so that using pandas data can be mapped to correct columns.

我想在列表的第一个位置插入一个像“xyz”这样的字符串,以便使用Pandas数据可以映射到正确的列。

回答by Adi219

Let's say that the second list is called array.

假设第二个列表称为array

You can use array.insert(0, "xyz")or array = ["xyz"] + arrayto add "xyz" to the beginning of the second list.

您可以使用array.insert(0, "xyz")array = ["xyz"] + array将“xyz”添加到第二个列表的开头。