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
How to convert python list to Pandas series
提问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 DataFrame
constructor with columns, then cast to string
and last add 0
by Series.str.zfill
if nested list
s:
首先使用DataFrame
构造的柱子,然后转换为string
和最后一个附加0
的Series.str.zfill
,如果嵌套list
S:
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 list
only:
如果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