简体   繁体   English

在Pandas Dataframe中要行的列表列表

[英]List of lists to row in Pandas Dataframe

I have a list of lists: 我有一个清单清单:

list = ['John', ['Jack', 'Michael', 'Ian'], ['Melissa', 'Hannah']]

Which I want to transform into a row of an existing Dataframe: 我想将其转换为现有数据框的一行:

Name         Children              Siblings
John     Jack, Michael, Ian     Melissa, Hannah

Does anyone have an idea on how to accomplish this? 有谁知道如何实现这一目标?

I'm new to Pandas and I have only found ways of converting lists into columns into a new dataframe for example: 我是Pandas的新手,我仅找到了将列表转换为列并转换为新数据框的方法,例如:

df = pd.DataFrame(letters).T
df.columns = [0, 1, 2]

print (df)
   0  1  2
0  a  b  c 

Just wrap it in brackets: 只需将其包装在方括号中即可:

arr = ['John', ['Jack', 'Michael', 'Ian'], ['Melissa', 'Hannah']]
df = pd.DataFrame([arr], columns=['Name', 'Children', 'Siblings'])

   Name              Children           Siblings
0  John  [Jack, Michael, Ian]  [Melissa, Hannah]

Since you mentioned you are appending to an existing dataframe: 既然您提到过,您将附加到现有数据框:

df

   Name              Children           Sublings
0  John  [Jack, Michael, Ian]  [Melissa, Hannah]

df.append(pd.DataFrame([arr], columns=df.columns)).reset_index(drop=True)

   Name              Children           Siblings
0  John  [Jack, Michael, Ian]  [Melissa, Hannah]
1  John  [Jack, Michael, Ian]  [Melissa, Hannah]

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

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