簡體   English   中英

熊貓數據框創建新列並使用第一列的值填充值

[英]pandas dataframe create new columns and fill values by using the values of the first column

我有一個熊貓數據框df ,它只有一列col 我想循環col值,並通過使用第一列col的值添加列以填充值。 例如,第一行是一個列表,其中包含3個元素['text1','text2','text3'] 我想添加3列,並使用'text1''text2''text3'填充值。

import pandas as pd

df=pd.DataFrame({'col':[['text1','text2','text3'],['mext1','mext2'],['cext1']]})
df

    col
0   [text1, text2, text3]
1   [mext1, mext2]
2   [cext1]

我想要這樣:

    col                     col_1     col_2     col_3
0   [text1, text2, text3]   text1     text2     text3
1   [mext1, mext2]          mext1     mext2     Nan
2   [cext1]                 cext1     Nan       Nan    

您的幫助將不勝感激。

您可以通過將單列中存在的值轉換為其list表示形式來構造新的數據框。 現在, list的元素本身將成為單獨的列實體。

然后可以將它們與原始DF逐列連接( axis=1)

df_expand = pd.DataFrame(df['col'].tolist(), df.index)
df_expand.columns = df_expand.columns + 1
pd.concat([df['col'], df_expand.add_prefix('col_')], axis=1)

在此處輸入圖片說明

要使None表示為NaN ,可以在最后一個語法的末尾添加.replace({None:np.NaN})

使用DataFrame構造函數的另一種解決方案,需要rename列和add_prefix

print (pd.DataFrame(df.col.values.tolist(), index=df.col)
         .rename(columns = lambda x: x+1)
         .add_prefix('col_')
         .reset_index())

                     col  col_1  col_2  col_3
0  [text1, text2, text3]  text1  text2  text3
1         [mext1, mext2]  mext1  mext2   None
2                [cext1]  cext1   None   None

解決方案,其中通過str.lencol列中找到列表的max長度:

cols = df.col.str.len().max() + 1
print (cols)
4
print (pd.DataFrame(df.col.values.tolist(), index=df.col,columns = np.arange(1, cols))
         .add_prefix('col_')
         .reset_index())
                     col  col_1  col_2  col_3
0  [text1, text2, text3]  text1  text2  text3
1         [mext1, mext2]  mext1  mext2   None
2                [cext1]  cext1   None   None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM