久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python pandas將列表插入單元格

Python pandas insert list into a cell(Python pandas將列表插入單元格)
本文介紹了Python pandas將列表插入單元格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個列表abc"和一個數(shù)據(jù)框df":

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

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

我想將列表插入單元格 1B,所以我想要這個結(jié)果:

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

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

我可以這樣做嗎?

1) 如果我使用這個:

1) If I use this:

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

我收到以下錯誤消息:

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) 如果我使用這個:

2) If I use this:

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

然后它會插入一個列表,其中只有一個元素是 'abc' 列表( [['foo', 'bar']] ).

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

3) 如果我使用這個:

3) If I use this:

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

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

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

4) 如果我使用這個:

4) If I use this:

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

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

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

感謝您的幫助!

我的新數(shù)據(jù)框和舊列表:

My new dataframe and the old list:

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

另一個數(shù)據(jù)框:

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

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

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

如果數(shù)據(jù)框的列僅包含整數(shù)值和/或 NaN 值和/或列表值,則將列表插入單元格可以完美地工作.如果數(shù)據(jù)框的列僅包含字符串值和/或 NaN 值和/或列表值,則將列表插入單元格可以完美地工作.但是,如果數(shù)據(jù)框包含具有整數(shù)和字符串值的列以及其他列,那么如果我使用它,則會出現(xiàn)錯誤消息: df2.loc[1,'B'] = abcdf3.loc[1,'B'] = abc.

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.

另一個數(shù)據(jù)框:

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

這些插入工作完美:df.loc[1,'B'] = abcdf4.loc[1,'B'] = abc..p>

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

推薦答案

df3.set_value(1, 'B', abc) 適用于任何數(shù)據(jù)幀.注意B"列的數(shù)據(jù)類型.例如.列表不能插入浮點列,在這種情況下 df['B'] = df['B'].astype(object) 可以提供幫助.

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.

這篇關(guān)于Python pandas將列表插入單元格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標)
主站蜘蛛池模板: 色羞羞| 国产精品欧美在线 | 欧美精品一 | 色婷婷久久综合 | 午夜国产福利 | 中文字幕一区在线观看 | 能看的av网站 | 日韩毛片网 | 成人欧美一区二区三区黑人孕妇 | 国产真实乱人偷精品 | 香蕉视频一区二区 | 色日韩 | 国产精品免费一区二区 | a级片网站 | 久久机热这里只有精品 | 青草在线视频 | 亚洲国产日韩欧美 | 亚洲成人精品在线 | 视频在线观看一区 | 中文在线观看视频 | 中文字幕在线一区二区三区 | 欧美精品一区二区三区四区 | 中文字幕在线观看一区二区三区 | 中文字幕av在线播放 | 国产精品99精品久久免费 | 欧美日韩亚洲一区 | 久久视频免费观看 | 日韩欧美国产高清91 | 在线免费黄色网址 | 中文字幕第2页 | 久艹视频在线观看 | 国产精品一区在线播放 | 亚洲天堂国产 | 中文字幕免费在线 | 91福利在线观看 | 国产免费91 | 日韩视频在线观看免费 | 伊人中文字幕 | www.狠狠操| 97在线免费 | 毛片毛片毛片 |