我在Pandas中使用布尔索引.问题是为什么声明:
a[(a['some_column']==some_number) & (a['some_other_column']==some_other_number)]
Run Code Online (Sandbox Code Playgroud)
工作正常,而
a[(a['some_column']==some_number) and (a['some_other_column']==some_other_number)]
Run Code Online (Sandbox Code Playgroud)
存在错误?
例:
a=pd.DataFrame({'x':[1,1],'y':[10,20]})
In: a[(a['x']==1)&(a['y']==10)]
Out: x y
0 1 10
In: a[(a['x']==1) and (a['y']==10)]
Out: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud) 我已经做了一些搜索,无法弄清楚如何过滤数据帧df["col"].str.contains(word),但是我想知道是否有办法做反向:按照该集合的赞美过滤数据帧.例如:对...的影响!(df["col"].str.contains(word)).
这可以通过一种DataFrame方法完成吗?
我经常处理格式不正确的数据(即数字字段不一致等)
可能还有其他方法,我不知道,但我在数据框中格式化单个列的方式是使用函数并将列映射到该函数.
format = df.column_name.map(format_number)
Run Code Online (Sandbox Code Playgroud)
问题:1 - 如果我有一个包含50列的数据框,并希望将该格式应用于多列,等等,如第1,3,5,7,9列,该怎么办?
你可以去吗:
format = df.1,3,5,9.map(format_number)
Run Code Online (Sandbox Code Playgroud)
..这样我可以在一行中格式化我的所有数字列?
我想在特定列中删除零值的行
>>> df
salary age gender
0 10000 23 1
1 15000 34 0
2 23000 21 1
3 0 20 0
4 28500 0 1
5 35000 37 1
Run Code Online (Sandbox Code Playgroud)
工资和年龄列中的一些数据缺失,第三列,性别是一个二元变量,1 表示男性 0 表示女性。这里的 0 不是缺失的数据,我想删除工资或年龄中的行,这样我就可以得到
>>> df
salary age gender
0 10000 23 1
1 15000 34 0
2 23000 21 1
3 35000 37 1
Run Code Online (Sandbox Code Playgroud)