简体   繁体   English

使用列表中的标签将 numpy 数组转换为 pandas dataframe

[英]Convert numpy array to pandas dataframe with labels from list

please advice how to perform the following premutations:请建议如何执行以下预突变:

array = [1, 3, 2] (numpy.ndarray)

l1 = ['foo_qwe1_ert1', 'bar_qwe2_ert2', 'baz_qwe3_ert3'] (list)

I need to get the following pandas dataframe:我需要得到以下 pandas dataframe:

Column1专栏1 Column2专栏2 Column3专栏3
foo qwe1 qwe1 ert1 ert1
baz巴兹 qwe3 qwe3 ert3 ert3
bar酒吧 qwe2 qwe2 ert2 ert2

the problem is the list contains text labels from 0 to 30(format: XXX_YYY_ZZZ) and numpy.array has shape (3536,) and contains numbers from 0 to 30. I need to assign label for each number in array and save it as pandas dataframe问题是列表包含从 0 到 30 的文本标签(格式:XXX_YYY_ZZZ)和 numpy.array 具有形状(3536,)并包含从 0 到 30 的数字。我需要为数组中的每个数字分配 label 并将其保存为 pandas dataframe

You can just use:您可以使用:

df = pd.DataFrame(data={'list':['foo_qwe1_ert1', 'bar_qwe2_ert2', 'baz_qwe3_ert3']})
df[['Column1', 'Column2', 'Column3']] = df['list'].str.split('_', expand=True)
df.drop(columns=['list'], inplace=True)

OUTPUT:

  Column1 Column2 Column3
0     foo    qwe1    ert1
1     bar    qwe2    ert2
2     baz    qwe3    ert3

First use:首次使用:

df = pd.DataFrame([x.split('_') for x in l1], columns=['Column1', 'Column2', 'Column3'])
print (df)
  Column1 Column2 Column3
0     foo    qwe1    ert1
1     bar    qwe2    ert2
2     baz    qwe3    ert3

You can use str.split and then reindex :您可以使用str.split然后reindex

df = pd.Series(l1).str.split('_', expand=True)
df.index = [1,2,3]
df = df.reindex(array).reset_index(drop=True).rename(columns={i:'Column'+str(i+1) for i in df.columns})

Output: Output:

  Column1 Column2 Column3
0     foo    qwe1    ert1
1     baz    qwe3    ert3
2     bar    qwe2    ert2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM