我有一个非常大的DataFrame,我想知道是否有短(一或两个班轮)方式来获取DataFrame中非NaN条目的数量.我不希望一次只做一列,因为我有接近1000列.
df1 = pd.DataFrame([(1,2,None),(None,4,None),(5,None,7),(5,None,None)],
columns=['a','b','d'], index = ['A', 'B','C','D'])
a b d
A 1 2 NaN
B NaN 4 NaN
C 5 NaN 7
D 5 NaN NaN
Run Code Online (Sandbox Code Playgroud)
输出:
a: 3
b: 2
d: 1
Run Code Online (Sandbox Code Playgroud) 我是Jupyter笔记本的新手.我养成了将部分代码保存在不同单元格中的习惯.现在,每次运行一个单元格时,就会在最后一个单元格之后添加一个新单元格.所以经过几次运行我有很多空白单元格.有没有办法限制Jupyter笔记本只在我们运行代码时保留一个黑色单元格(因为当最后一个单元格之后的旧黑色单元格存在时不生成新的空白单元格)?
屏幕截图:之后只保留一个空白单元格
来自utils import*
当我使用udf通过分隔符在列中拆分字符串时,我一直收到错误.我正在使用Scala
Error: java.lang.ClassCastException: $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$anonfun$1 cannot be cast to scala.Function2
Run Code Online (Sandbox Code Playgroud)
不知道这是什么以及如何解决它.
这是我的udf和数据框架:
val rsplit = udf((refsplit: String) => refsplit.split(":"))
+---------+--------------------+--------------------+
| user| jsites| jsites1|
+---------+--------------------+--------------------+
|123ashish|m.mangahere.co:m....|m.mangahere.co:m....|
|456ashish|m.mangahere2.co:m...|m.mangahere2.co:m...|
| ashish|m.mangahere.co:m....|m.mangahere.co:m....|
+---------+--------------------+--------------------+
Run Code Online (Sandbox Code Playgroud)
列jsites看起来像m.manghere.co:m.facebook.com:.msn.com.而我试图使用UDF分裂m.manghere.co:m.facebook.com:.msn.com的:.
我一直在收到这个错误
我想在df中添加一列.这个新df的值将取决于其他列的值.例如
dc = {'A':[0,9,4,5],'B':[6,0,10,12],'C':[1,3,15,18]}
df = pd.DataFrame(dc)
A B C
0 0 6 1
1 9 0 3
2 4 10 15
3 5 12 18
Run Code Online (Sandbox Code Playgroud)
现在我想添加另一个列D,其值取决于A,B,C的值.所以例如,如果迭代通过df,我会这样做:
for row in df.iterrows():
if(row['A'] != 0 and row[B] !=0):
row['D'] = (float(row['A'])/float(row['B']))*row['C']
elif(row['C'] ==0 and row['A'] != 0 and row[B] ==0):
row['D'] == 250.0
else:
row['D'] == 20.0
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有for循环或使用where()或apply()函数的情况下执行此操作.
谢谢
我想知道是否有人知道一个 python 包,它实现 MLE 来估计狄利克雷分布的参数。
有谁知道如何使用 Tweepy(Python Twitter api 包装器)找到所有收藏/喜欢给定推文 ID 的推文的用户?
我可以得到转发,但不能得到收藏。我看到它api.favorite存在,但那里的响应是二进制的。
我正在使用Mountain Lion.我安装了python 3.3也有python 2.7.我用的是自制软件.
打开python时,我得到:
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
我试过这样做:
brew switch python3 3.3.0.
仍然没有奏效.有任何想法吗?
我有一个列表,我想使用 Python 将该列表的每个元素输入到 MYSQL 列的关联索引单元格中。
例如
lst = [11,22,33,44,55,66]
Run Code Online (Sandbox Code Playgroud)
MYSql 列:
Data
11
22
33
44
55
66.
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点。
我有两个数据框,我想将它们乘以索引.最好的方法是什么?注意:列名称不同.
df1 = pd.DataFrame([(1,2,3),(3,4,5),(5,6,7)], columns=['a','b','d'], index = ['A', 'B','C'])
df1
a b d
A 1 2 3
B 3 4 5
C 5 6 7
df2 = pd.DataFrame([(10,20,30)], columns=['A','B','C'],index = ['ss'])
df2 = df2.transpose()
df2
ss
A 10
B 20
C 30
Run Code Online (Sandbox Code Playgroud)
输出数据帧:
a b d
A 10 20 30
B 60 80 100
C 150 180 210
Run Code Online (Sandbox Code Playgroud) python ×7
dataframe ×4
pandas ×3
apache-spark ×1
api ×1
cell ×1
count ×1
dirichlet ×1
homebrew ×1
jupyter ×1
macos ×1
mle ×1
mysql ×1
mysql-python ×1
nan ×1
numpy ×1
python-2.7 ×1
python-3.x ×1
scala ×1
statistics ×1
tweepy ×1
twitter ×1