[英]pandas: Buffer has wrong number of dimensions (expected 1, got 0) when transforming a dataframe column of sets
I have a dataframe with a column containing one-element sets. 我有一个包含一个元素集的列的数据框。 The one-elements are always of type str
. 单元素总是str
类型。 I want to change this column's values to that one str
inside each set. 我想将此列的值更改为每个集合中的一个str
。
My understanding of pandas so far has lead me to use the transform
function on my dataframe. 到目前为止,我对熊猫的理解使我在我的数据帧上使用了transform
函数。 Because I only want to change one column, I call it on that column specifically. 因为我只想更改一列,所以我专门在该列上调用它。
I've tried map
, transform
, and apply
(I can't(?) use applymap because the column is simply a series). 我已经尝试过map
, transform
和apply
(我不能(?)使用applymap,因为该列只是一个系列)。 Examples of what I've tried is this: 我尝试过的例子如下:
df['foo'].map(''.join)
df['foo'].transform(''.join)
df['foo'].apply(''.join)
this returns my column as a series, but doesn't change df
's foo
column's values in-place. 这会将我的列作为一个系列返回,但不会就地更改df
的foo
列的值。 I then tried these: 然后我尝试了这些:
df['foo'] = df['foo'].map(''.join)
df['foo'] = df['foo'].transform(''.join)
df['foo'] = df['foo'].apply(''.join)
swap out ''.join
for lambda x: next(iter(x))
and the same error occurs. 换掉''.join
for lambda x: next(iter(x))
并发生同样的错误。
some sample data from head(df)
(thanks for the suggestion Yuca): 来自head(df)
一些样本数据(感谢Yuca的建议):
treatment AAA AAA AAA ... BBB BBB foo
15025 0.0 0.0 0.0 ... 126250.0 191730.0 {K00134}
6746 1523000.0 2374300.0 1394500.0 ... 536750.0 1179000.0 {K00134}
11097 1146200.0 423280.0 316280.0 ... 0.0 115880.0 {K00895}
3098 193000.0 57943.0 94483.0 ... 0.0 191530.0 {K01689}
14797 53168.0 0.0 65262.0 ... 0.0 0.0 {K00169}
[5 rows x 9 columns]
I think my understanding of apply
, map
, transform
, etc. needs some work, but I just want to figure out how to do this and maybe why my method doesn't work. 我认为我对apply
, map
, transform
等的理解需要一些工作,但我只想弄清楚如何做到这一点,也许我的方法不起作用。 Thanks! 谢谢!
fix : as @Yuca mentioned, I had duplicate column names. 修复 :正如@Yuca所提到的,我有重复的列名。 This was because I had earlier removed 2/3 levels from the original MultiIndex assigned to df
's columns. 这是因为我之前从分配给df
列的原始MultiIndex中删除了2/3级别。 When I re-assigned the columns with unique names, the lines with =
worked! 当我重新分配具有唯一名称的列时,带有=
的行已经工作! Thanks everyone for your help. 谢谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.