pandas 如何将python列表转换为Pandas系列

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

How to convert python list to Pandas series

pythonpandas

提问by Christina Hughes

I have a python list l.The first few elements of the list looks like below

我有一个 python 列表 l.列表的前几个元素如下所示

[751883787]
[751026090]
[752575831]
[751031278]
[751032392]
[751027358]
[751052118]

I want to convert this list to pandas.core.series.Series with 2 leading 0.My final outcome will look like

我想将此列表转换为带有 2 个前导 0 的 pandas.core.series.Series。我的最终结果将如下所示

00751883787
00751026090
00752575831
00751031278
00751032392
00751027358
00751052118

I'm working in Python 3.x in windows environment.Can you suggest me how to do this? Also my list contains around 2000000 elements

我正在 Windows 环境中使用 Python 3.x。你能建议我怎么做吗?我的列表也包含大约 2000000 个元素

回答by riccardo nizzolo

you can try:

你可以试试:

list=[121,123,125,145]
series='00'+pd.Series(list).astype(str)
print(series)

output:

输出:

0    00121
1    00123
2    00125
3    00145
dtype: object

回答by jezrael

First use DataFrameconstructor with columns, then cast to stringand last add 0by Series.str.zfillif nested lists:

首先使用DataFrame构造的柱子,然后转换为string和最后一个附加0Series.str.zfill,如果嵌套listS:

lst = [[751883787],
       [751026090],
       [752575831],
       [751031278],
       [751032392],
       [751027358],
       [751052118]]

s = pd.DataFrame(lst, columns=['a'])['a'].astype(str).str.zfill(11)
print (s)
0    00751883787
1    00751026090
2    00752575831
3    00751031278
4    00751032392
5    00751027358
6    00751052118
Name: a, dtype: object


If there is one listonly:

如果list只有一个:

lst = [751883787,
       751026090,
       752575831,
       751031278,
       751032392,
       751027358,
       751052118]


s = pd.Series(lst).astype(str).str.zfill(11)
print (s)
0    00751883787
1    00751026090
2    00752575831
3    00751031278
4    00751032392
5    00751027358
6    00751052118
dtype: object

回答by jpp

This is one way.

这是一种方式。

from itertools import chain; concat = chain.from_iterable
import pandas as pd

lst = [[751883787],
       [751026090],
       [752575831],
       [751031278]]

pd.DataFrame({'a': pd.Series([str(i).zfill(11) for i in concat(lst)])})

             a
0  00751883787
1  00751026090
2  00752575831
3  00751031278

Some benchmarking, relevant since your dataframe is large:

一些基准测试,因为你的数据框很大:

from itertools import chain; concat = chain.from_iterable
import pandas as pd

lst = [[751883787],
       [751026090],
       [752575831],
       [751031278],
       [751032392],
       [751027358],
       [751052118]]*300000

%timeit pd.DataFrame(lst, columns=['a'])['a'].astype(str).str.zfill(11)
# 1 loop, best of 3: 7.88 s per loop

%timeit pd.DataFrame({'a': pd.Series([str(i).zfill(11) for i in concat(lst)])})
# 1 loop, best of 3: 2.06 s per loop

回答by krock1516

both the given answers are usefull ... below is the summrise one

给出的答案都很有用......下面是总结之一

import pandas as pd
mylist = [751883787,751026090,752575831,751031278]
mysers = pd.Series(mylist).astype(str).str.zfill(11)
print (mysers)

./test
0    00751883787
1    00751026090
2    00752575831
3    00751031278
dtype: object

another way around is , cast the dtype of the series to str using astype and use vectorised str.zfill to pad with 00, though using lamda will be more easy to read ..

另一种解决方法是,使用 astype 将系列的 dtype 转换为 str 并使用矢量化 str.zfill 填充 00,尽管使用 lamda 会更容易阅读..

import pandas as pd
mylist = pd.DataFrame([751883787,751026090,752575831,751031278], columns=['coln'])
result = mylist.coln.apply(lambda x: str(int(x)).zfill(11))
print(result)

Below is the result..

下面是结果。。

./test
0    00751883787
1    00751026090
2    00752575831
3    00751031278
Name: coln, dtype: object