2024-06-28 18:45:03 发布
网友
所以我在pandas中有一个包含许多列的数据帧。在
一列有一个列表,其中的字符串以[u'str',]结尾,如下所示。每行中的字符串数目不相等。在
column x [u'str1', u'str2', u'str3'] [u'str4', u'str1'] [u'str5', u'str7', u'str8', u'str9']
我想在dataframe中创建新的列,称为column x-1,column x-2直到x-n
如何:
所以这个问题的确切代码是:
df_test['actors_list'] = df_m.actors_list.str.split('u\'') #splits based on deliminator u' (the \ is the escape character) df_test2 = pd.DataFrame( df_test['actors_list'].tolist()).rename(lambda x: 'actors_list-{}'.format(x + 1), axis=1) df_test2
如果“column x”是列表的列,则可以将该列作为一个序列传递,以创建新的数据帧。在
df['column x'] 0 [a, b, c] 1 [d] 2 [e, f] dtype: object df2 = pd.DataFrame( df['column x'].tolist()).rename(lambda x: 'x-{}'.format(x + 1), axis=1) df2 x-1 x-2 x-3 0 a b c 1 d None None 2 e f None
要将这些列添加回df,请使用pd.concat:
df
pd.concat
所以这个问题的确切代码是:
如果“column x”是列表的列,则可以将该列作为一个序列传递,以创建新的数据帧。在
要将这些列添加回
^{pr2}$df
,请使用pd.concat
:相关问题 更多 >
编程相关推荐