Python大 pandas 将列表插入单元格 [英] Python pandas insert list into a cell

查看:303
本文介绍了Python大 pandas 将列表插入单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表'abc'和一个数据框'df':

  abc = ['foo','bar '] 
df =
AB
0 12 NaN
1 23 NaN

我想将列表插入单元格1B,所以我想要这个结果:

  AB 
0 12 NaN
1 23 ['foo','bar']

我这样做?



1)如果我使用这个:

  df.ix [1,'B'] = abc 

我收到以下错误消息: p $

  ValueError:设置一个可迭代的
时必须具有相同的len键和值*

因为它尝试将列表(有两个元素)插入行/列,但不能插入到单元格中。



2)如果我使用这个:

  df.ix [1,'B'] = [abc] 

然后它插入一个只有一个元素是abc列表的列表( [['foo','bar']] )。



3)如果我使用这个:

  df.ix [1,'B'] =','.join(abc)

然后它插入一个字符串:( foo,bar ),但不是列表。



4)如果我使用这个:

  df.ix [1,'B'] = [','.join(abc)] 

然后它插入一个列表,但它只有一个元素( ['foo,bar'] ),但不是我想要的两个( ['foo','bar']




编辑



我的新数据框和旧列表:

  abc = 'foo','bar'] 
df2 =
ABC
0 12 NaN'bla'
1 23 NaN'bla bla'

另一个数据框:

  df3 = 
ABCD
0 12 NaN'bla'['item1','item2']
1 23 NaN'bla bla'[11,12,13]

我想插入'abc'列表 df2.loc [1,'B'] 和/或 df3.loc [1,'B']

如果数据框只包含整数值和/或NaN值和/或列表值,则将列表插入到单元格中可以完美工作。如果数据帧仅具有字符串值和/或NaN值和/或列表值的列,则将列表插入到单元格中将完美工作。但是如果数据框具有整数和字符串值和其他列的列,那么如果我使用这个列,则出现错误消息: df2.loc [1,'B'] = abc df3.loc [1,'B'] = abc



另一个数据框:

  df4 = 
AB
0'bla'NaN
1'bla bla'NaN

这些插入功能完美: df.loc [1,'B'] = abc df4.loc [1,'B'] = abc

解决方案

df3.set_value(1,'B',abc)适用于任何数据帧。处理列B的数据类型。例如。一个列表不能插入一个浮动列,在这种情况下 df ['B'] = df ['B']。astype(object)可以帮助。 p>

I have a list 'abc' and a dataframe 'df':

abc = ['foo', 'bar']
df =
    A  B
0  12  NaN
1  23  NaN

I want to insert the list into cell 1B, so I want this result:

    A  B
0  12  NaN
1  23  ['foo', 'bar']

Ho can I do that?

1) If I use this:

df.ix[1,'B'] = abc

I get the following error message:

ValueError: Must have equal len keys and value when setting with an iterable

because it tries to insert the list (that has two elements) into a row / column but not into a cell.

2) If I use this:

df.ix[1,'B'] = [abc]

then it inserts a list that has only one element that is the 'abc' list ( [['foo', 'bar']] ).

3) If I use this:

df.ix[1,'B'] = ', '.join(abc)

then it inserts a string: ( foo, bar ) but not a list.

4) If I use this:

df.ix[1,'B'] = [', '.join(abc)]

then it inserts a list but it has only one element ( ['foo, bar'] ) but not two as I want ( ['foo', 'bar'] ).

Thanks for help!


EDIT

My new dataframe and the old list:

abc = ['foo', 'bar']
df2 =
    A    B         C
0  12  NaN      'bla'
1  23  NaN  'bla bla'

Another dataframe:

df3 =
    A    B         C                    D
0  12  NaN      'bla'  ['item1', 'item2']
1  23  NaN  'bla bla'        [11, 12, 13]

I want insert the 'abc' list into df2.loc[1,'B'] and/or df3.loc[1,'B'].

If the dataframe has columns only with integer values and/or NaN values and/or list values then inserting a list into a cell works perfectly. If the dataframe has columns only with string values and/or NaN values and/or list values then inserting a list into a cell works perfectly. But if the dataframe has columns with integer and string values and other columns then the error message appears if I use this: df2.loc[1,'B'] = abc or df3.loc[1,'B'] = abc.

Another dataframe:

df4 =
          A     B
0      'bla'  NaN
1  'bla bla'  NaN

These inserts work perfectly: df.loc[1,'B'] = abc or df4.loc[1,'B'] = abc.

解决方案

df3.set_value(1, 'B', abc) works for any dataframe. Take care of the data type of column 'B'. Eg. a list can not be inserted into a float column, at that case df['B'] = df['B'].astype(object) can help.

这篇关于Python大 pandas 将列表插入单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆