pandas 如何根据列表中的项目复制熊猫中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15952291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:45:40  来源:igfitidea点击:

How to duplicate rows in pandas, based on items in a list

pythonpandas

提问by vgoklani

I have a pandas dataframe that looks like this:

我有一个如下所示的 Pandas 数据框:

COL     data
line1   [A,B,C]

where the items in the data column could either be a list or just comma separated elements. Is there an easyof way of getting:

其中数据列中的项目可以是列表,也可以只是逗号分隔的元素。有没有简单的获取方式:

COL     data
line1   A
line1   B
line1   C

I could iterate over the list and manually duplicate the rows via python, but is there some magicpandas trick for doing this? The key point is how to automatically duplicate the rows.

我可以遍历列表并通过 python 手动复制行,但是是否有一些魔术Pandas技巧可以做到这一点?关键是如何自动复制行。

Thanks!

谢谢!

采纳答案by Andy Hayden

You could write a simple cleaning function to make it a list (assuming it's not a list of commas, and you can't simply use ast.literal_eval):

您可以编写一个简单的清理函数来使其成为一个列表(假设它不是一个逗号列表,并且您不能简单地使用ast.literal_eval):

def clean_string_to_list(s):
    return [c for c in s if c not in '[,]']  # you might need to catch errors

df['data'] = df['data'].apply(clean_string_to_list)

Iterating through the rows seems like a reasonable choice:

遍历行似乎是一个合理的选择:

In [11]: pd.DataFrame([(row['COL'], d)
                       for d in row['data']
                       for _, row in df.iterrows()],
                       columns=df.columns)
Out[11]:
     COL data
0  line1    A
1  line1    B
2  line1    C

I'm afraid I don't think pandas caters specifically for this kind of manipulation.

恐怕我不认为Pandas专门针对这种操纵。