将系列转换为每个字符的列表

huangapple go评论67阅读模式
英文:

Series into list of each character

问题

result 

output:
[1, 0, 0, 1, 1]
英文:

I have a pandas series element like this:

test

output:
1    10011
Name: working_days, dtype: object

and I want to store each number as an element in a list like this:

result 

output:
[1,0,0,1,1]

Could you help me with the transformation without getting this as a result:

[10011]

答案1

得分: 2

假设您有字符串(数据类型为对象),您可以使用以下代码:

s = pd.Series(['10011'], name='working_days')

out = s.str.findall(r'\d')

或者,如果您只有数字(@AbdulNiyasPM 的建议):

out = s.map(list)

输出:

0    [1, 0, 0, 1, 1]
Name: working_days, dtype: object
英文:

Assuming you have strings (your dtype is object), you could use:

s = pd.Series(['10011'], name='working_days')

out = s.str.findall(r'\d')

Or, if you only have digits (suggestion from @AbdulNiyasPM):

out = s.map(list)

Output:

0    [1, 0, 0, 1, 1]
Name: working_days, dtype: object

答案2

得分: 1

你可以将一个函数应用到你的系列上,该函数会将数字拆分并将它们放入一个列表中:

test.apply(lambda x: list(map(int, str(x))))
英文:

You can apply a function to your series that will split the digits and put them in a list:

test.apply(lambda x: list(map(int, str(x))))

答案3

得分: 1

将数字首先转换为字符串,然后创建一个包含该字符串的列表,这将字符串拆分为单个字符。

英文:

Even simpler:

import pandas as pd
s = pd.Series(['10011'], name='working_days')

s.apply(lambda x: list(str(x)))

cast the number as a string first, then create a list with the string, which splits the string into individual characters

答案4

得分: 1

你可以使用NumPy的视图功能:

test.values.astype(str)[:, None].view('<U1').astype(int).tolist()
[[1, 0, 0, 1, 1],
 [0, 0, 1, 1, 0]]

然而,如果你的Series中只有一个元素,你可以简单地这样做:

test.values.astype(str).view('<U1').astype(int).tolist()
[1, 0, 0, 1, 1]

另外,如果你想将这个列表分解成5列,以表示工作日:

import pandas as pd
import numpy as np

days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
test = pd.Series(['10011', '00110'])

df = pd.DataFrame(test.values.astype(str)[:, None].view('<U1').astype(int), columns=days)

输出结果:

test
0    10011
1    00110
dtype: object

df
  Mon Tue Wed Thu Fri
0   1   0   0   1   1
1   0   0   1   1   0
英文:

You can use numpy view:

&gt;&gt;&gt; test.values.astype(str)[:, None].view(&#39;&lt;U1&#39;).astype(int).tolist()
[[1, 0, 0, 1, 1],
 [0, 0, 1, 1, 0]]

However if you have only one element in your Series, you can simply do:

&gt;&gt;&gt; test.values.astype(str).view(&#39;&lt;U1&#39;).astype(int).tolist()
[1, 0, 0, 1, 1]

Alternative if you want to explode this list into 5 columns for working days:

import pandas as pd
import numpy as np

days = [&#39;Mon&#39;, &#39;Tue&#39;, &#39;Wed&#39;, &#39;Thu&#39;, &#39;Fri&#39;]
test = pd.Series([&#39;10011&#39;, &#39;00110&#39;])

df = pd.DataFrame(test.values.astype(str)[:, None].view(&#39;&lt;U1&#39;).astype(int), columns=days)

Output:

&gt;&gt;&gt; test
0    10011
1    00110
dtype: object

&gt;&gt;&gt; df
  Mon Tue Wed Thu Fri
0   1   0   0   1   1
1   0   0   1   1   0

答案5

得分: 0

另一种可能的解决方案:

s.astype(str).str.split(r'(?<=\d)(?=\d)', expand=True).apply(list, axis=1)

或者,

s.astype(str).str.split(r'(?<=\d)(?=\d)', expand=True).values.astype(int).tolist()

输出:

# 第一种解决方案
0    [1, 0, 0, 1, 1]
dtype: object

# 第二种解决方案
[[1, 0, 0, 1, 1]]
英文:

Another possible solution:

s.astype(str).str.split(r&#39;(?&lt;=\d)(?=\d)&#39;, expand=True).apply(list, axis=1)

Alternatively,

s.astype(str).str.split(r&#39;(?&lt;=\d)(?=\d)&#39;, expand=True).values.astype(int).tolist()

Output:

# First solution
0    [1, 0, 0, 1, 1]
dtype: object

# Second solution
[[1, 0, 0, 1, 1]]

huangapple
  • 本文由 发表于 2023年6月15日 20:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定