- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试对多 (4) 列执行 explode
时遇到一些问题。第一个问题是,如果我尝试一次展开所有列,我会遇到 MemoryError
。分别分解每一列后有很多重复项,因此我可以使用 drop_duplicates()
,但是由于列中有 lists
,它引发了 TypeError: unhashable type: '列表'
。如果我使用 astype(str)
将列转换为字符串,则这些列不能与 .explode()
一起使用。因此,如果我在执行第二个 .explode()
之前尝试 pd.eval()
列,我会得到 UndefinedVariableError: name 'nan' is not defined
。这是示例数据集:
id col_1 col_2 col_3 col_4
0 1 ['a','b'] nan ['c'] nan
1 2 nan ['d','e'] nan nan
2 3 ['f'] nan nan nan
3 4 nan ['g'] nan nan
4 5 nan nan ['h'] nan
5 6 nan nan ['i'] ['j']
这是当前代码:
for i in new_table:
new_table = new_table.explode(i)
new_table = new_table.astype(str)
new_table = new_table.drop_duplicates()
new_table['col_1'] = pd.eval(new_table['col_1'])
new_table['col_2'] = pd.eval(new_table['col_2'])
new_table['col_3'] = pd.eval(new_table['col_3'])
new_table['col_4'] = pd.eval(new_table['col_4'])
pd.eval()
引发了 UndefinedVariableError: name 'nan' is not defined
。如果我删除最后 4 行,则这些列将被解释为字符串,并且在第二个循环中,explode()
不会执行任何操作,因为输入是字符串,而不是列表。但是我必须(?)将列转换为字符串以执行drop_duplicates()
。
重新创建示例数据集的代码:
new_table = pd.DataFrame({'id':[1,2,3,4,5,6],
'col_1':[['a','b'],np.nan,['f'],np.nan,np.nan,np.nan],
'col_2':[np.nan,['d','e'],np.nan,['g'],np.nan,np.nan],
'col_3':[['c'],np.nan,np.nan,np.nan,['h'],['i']],
'col_4':[np.nan,np.nan,np.nan,np.nan,np.nan,['j']]})
预期输出:
id col_1 col_2 col_3 col_4
1 a nan c nan
1 b nan c nan
2 nan d nan nan
2 nan e nan nan
3 f nan nan nan
4 nan g nan nan
5 nan nan h nan
6 nan nan i j
最佳答案
你能不能像这样:
df[['id']].join((df[i].explode() for i in df.iloc[:,1:]))
输出:
| | id | col_1 | col_2 | col_3 | col_4 |
|---:|-----:|:--------|:--------|:--------|:--------|
| 0 | 1 | a | nan | c | nan |
| 0 | 1 | b | nan | c | nan |
| 1 | 2 | nan | d | nan | nan |
| 1 | 2 | nan | e | nan | nan |
| 2 | 3 | f | nan | nan | nan |
| 3 | 4 | nan | g | nan | nan |
| 4 | 5 | nan | nan | h | nan |
| 5 | 6 | nan | nan | i | j |
注意,我认为你在做什么和我在做什么的主要区别在于你在数据框上使用 explode
,因此你的数据框对于你调用的每一列都是重复的.然后,您只选择“分解的”columna 并加入新的数据框。
我正在做的是分解每一列 (pd.Series) 并将每个“分解”系列的结果连接到索引上。我不会创建一堆额外的列,这些列是在数据框上使用 explode 时创建的。
关于python - Pandas 爆炸并删除多列的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60152241/
我正在尝试在 Pyspark 中实现自定义爆炸。我有 4 列,它们是具有几乎相同架构的结构数组(一列结构包含的字段比其他三列少一个)。 对于我的 DataFrame 中的每一行,我有 4 列是结构数组
我如何分解 [group_concat(DISTINCT LineItem.ItemID)] => 600278,WH1502。 我试过了,但什么也没显示 $result = mysqli_query
假设我有一个数据框 df : C1 C2 C3 C4 C5 0 [A] [1] s1 [123]
我的数据在其中一个字段中具有嵌套映射。例如,数据位于名为“customers”的表中,如下所示: 姓名:比尔·琼斯 地址:{"billing":{"street":"123 Main", "city"
我有两个独立的项目(ejb,web),完全独立。问题是我想通过maven将ejb项目添加到web依赖项,但我正在寻找一个爆炸依赖项而不是jar,所以当我编译ejb项目时,更改会立即在web项目中生效。
我正在将我的 ROOT.war 文件复制到 Tomcat 6.0 的 webapps 中。有没有一种方法可以在复制时分解文件。即当我的脚本在 webapps 中复制它时..它应该爆炸并创建 ROOT
我有一个体素世界,类似于 Minecraft。每个体素( block )的大小为 1x1x1。 我想计算具有给定爆炸半径的特定位置的爆炸破坏,这意味着游戏将在该位置周围破坏体素(给定所需的半径)。 意
在我的游戏中有一辆坦克。我有一个扩展 Sprite 的 Barrel 类,这个类负责处理与桶相关的东西,其中之一就是射击时的爆炸动画。这就是我尝试这样做的方式: batch.draw(currShot
我一直在严重依赖 CSS 来创建我正在开发的网站。现在,所有 CSS 样式都在每个标签的基础上应用,所以现在我试图将其移动到更多的外部样式以帮助将来进行任何更改。 但现在的问题是我注意到我收到了“CS
我有一个 NSString:@"1a,1b,1c,1d,5c"。我想把这个 NSString 分成一个 NSMutableArray,但我不知道如何。我认为它相当简单,但我找不到它(也许是因为我的英语
我正在尝试使用 QML Canvas.requestAnimationFrame 来绘制一些自定义动画。我预计所提供的回调每帧调用一次,大约每秒 60 次。我的代码是: Canvas { id
我有: df = pd.DataFrame({'col1': ['asdf', 'xy', 'q'], 'col2': [1, 2, 3]}) col1 col2 0 asdf 1
在对 this question 的回答中MathWorks 支持团队声明: It is also possible to overload the ! (bang) operator by crea
好吧——穿上你 70 年代的喇叭裤,然后走进 WayBack 机器……迪斯科又活了,伙计们……(我怀疑这有标签……) 我需要 UUCP 的继任者。对你们这些 child 来说,在互联网出现之前和拜赛因
我今天阅读的一个问题的答案建议将应用程序部署为展开(解压缩)WAR .这让我开始思考。 我对 JBoss/Tomcat 所做的每一次部署都使用了 WAR/EAR文件。至少在我的内存中。 这些年来我一直
我有一个包含以下内容的 Spark Dataframe: 姓名E1E2E3abc456 我需要各种 E列成为新列中的行,如下所示: 姓名值E类型abc4E1abc5E2abc6E3 This answ
$user = $this->user; $user->name = $request['name']; $user->email = $request
我正在使用 Java 1.6、JBoss 5.1、EJB 3 和 Hibernate 2。每次部署 ear 时,如果 jar 是紧凑型(未展开),应用程序将无法运行。但是,当我炸开 jar 然后将其放
我是 dask 的新手,请多多包涵。 我有一个 JSON 文件,其中每一行都有以下架构: { 'id': 2, 'version': 7.3, 'participants': range(10)
我尝试使用 Chai Should 风格的断言,但是下面的语句让 Mocha 爆炸了。这是我完整的 Require block : /**
我是一名优秀的程序员,十分优秀!