Python 如何将等长字符串分解为数据帧列(没有空列)
我有一个数据帧,字符串长度相等(10)。我想把这一栏“分解”成10栏。无论我尝试什么解决方案,都会有一个前导的空列。现有的解决方案给了我这个问题,所以不用说这个问题的现有答案并不令人满意Python 如何将等长字符串分解为数据帧列(没有空列),python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据帧,字符串长度相等(10)。我想把这一栏“分解”成10栏。无论我尝试什么解决方案,都会有一个前导的空列。现有的解决方案给了我这个问题,所以不用说这个问题的现有答案并不令人满意 import pandas as pd df = pd.DataFrame(['tenletters', 'alsotenten', 'letter10!!', 'ten10lette'], columns=['col1']) df2 = pd.DataFrame(df['col1'].str.s
import pandas as pd
df = pd.DataFrame(['tenletters', 'alsotenten', 'letter10!!', 'ten10lette'],
columns=['col1'])
df2 = pd.DataFrame(df['col1'].str.split('').tolist())
如何以正确的方式(即没有前导的空列)执行此操作?使用map
df_final = pd.DataFrame(df['col1'].map(list).tolist())
Out[44]:
0 1 2 3 4 5 6 7 8 9
0 t e n l e t t e r s
1 a l s o t e n t e n
2 l e t t e r 1 0 ! !
3 t e n 1 0 l e t t e
使用map
df_final = pd.DataFrame(df['col1'].map(list).tolist())
Out[44]:
0 1 2 3 4 5 6 7 8 9
0 t e n l e t t e r s
1 a l s o t e n t e n
2 l e t t e r 1 0 ! !
3 t e n 1 0 l e t t e
你用
你可以试试看。(不是性能解决方案)
使用
注意:该列为多索引
,使其成为一个单级df.columns=d.columns。获取您使用的\u-level\u值(1)
你可以试试看。(不是性能解决方案)
使用
注意:该列是
多索引
以使其成为单个级别df.columns=d.columns.get_level_values(1)
该.values
似乎是冗余的值
似乎是冗余的
df_final = pd.DataFrame(df['col1'].map(list).tolist())
Out[44]:
0 1 2 3 4 5 6 7 8 9
0 t e n l e t t e r s
1 a l s o t e n t e n
2 l e t t e r 1 0 ! !
3 t e n 1 0 l e t t e
df.col1.apply(lambda x: pd.Series(list(x)))
0 1 2 3 4 5 6 7 8 9
0 t e n l e t t e r s
1 a l s o t e n t e n
2 l e t t e r 1 0 ! !
3 t e n 1 0 l e t t e
df.col1.str.extractall(r'(.)').unstack()
0
match 0 1 2 3 4 5 6 7 8 9
0 t e n l e t t e r s
1 a l s o t e n t e n
2 l e t t e r 1 0 ! !
3 t e n 1 0 l e t t e