英文:
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:
>>> test.values.astype(str)[:, None].view('<U1').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:
>>> test.values.astype(str).view('<U1').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 = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
test = pd.Series(['10011', '00110'])
df = pd.DataFrame(test.values.astype(str)[:, None].view('<U1').astype(int), columns=days)
Output:
>>> 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
答案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'(?<=\d)(?=\d)', expand=True).apply(list, axis=1)
Alternatively,
s.astype(str).str.split(r'(?<=\d)(?=\d)', expand=True).values.astype(int).tolist()
Output:
# First solution
0 [1, 0, 0, 1, 1]
dtype: object
# Second solution
[[1, 0, 0, 1, 1]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论