Arrays 使用dataframe进行数据操作
我有MxN numpy阵列数据。 它们在csv文件中,我正在使用pandas模块进行读取Arrays 使用dataframe进行数据操作,arrays,python-2.7,pandas,numpy,Arrays,Python 2.7,Pandas,Numpy,我有MxN numpy阵列数据。 它们在csv文件中,我正在使用pandas模块进行读取 sv-01 sv-02 SV-03 state-01 state-02 state-03 val-01 val-02 val-03 7 12 8 B B B .23 0.34 1.03 7 12 8 B B A .3
sv-01 sv-02 SV-03 state-01 state-02 state-03 val-01 val-02 val-03
7 12 8 B B B .23 0.34 1.03
7 12 8 B B A .35 0.10 0
7 12 8 B A A 1.45 0 0
7 12 8 A A A 0 0 0
7 12 8 A B B 0 1.23 3.21
为了便于计算,我需要两个变量。如果状态为A,则在每行中
那么相应的卫星就不会考虑了。所以第一排的总SV
是3。对于其他行,类似地为2,1,0,1。另一个变量是count的数量。
如果任何行没有单个B,则该行将不被计算。
最后一行将被视为2,因为存在两个B
#So my expected output is
#Total count is 4
#and number of satellites used is 3,2,1,0,1 in each row or iteration
如何迭代我的数据
首先感谢并将所有数据与A
进行比较,求和True
s,最后减去B
:
详情:
print (df.filter(like='state'))
state-01 state-02 state-03
0 B B B
1 B B A
2 B A A
3 A A A
4 A B B
print (df.filter(like='state').eq('A'))
state-01 state-02 state-03
0 False False False
1 False False True
2 False True True
3 True True True
4 True False False
对于计数所有非A
行,使用至少一个True
和sum
进行检查:
b = df.filter(like='state').eq('A').any(1).sum()
print (b)
4
print (df.filter(like='state').eq('A').any(1))
0 False
1 True
2 True
3 True
4 True
dtype: bool
总而言之:
mask = df.filter(like='state').eq('A')
a = mask.sum(axis=1).rsub(3)
print (a)
0 3
1 2
2 1
3 0
4 2
dtype: int64
b = mask.any(1).sum()
print (b)
4
最后一行是否应计为2?你在计算国家专栏中的B吗?@jezrael我有两个疑问。1。什么是“喜欢”。2.以及如何计算计数总数。在这里是4,因为在第4行中没有B。
like
表示检查列nmaes以查找状态
。对于比较数字,然后计数df.filter(比如class='state').eq(100).sum(1)
我为b
@Jezrael添加了解决方案。谢谢我会一步一步地研究你的解决方案,很高兴能帮上忙!天气真好!
mask = df.filter(like='state').eq('A')
a = mask.sum(axis=1).rsub(3)
print (a)
0 3
1 2
2 1
3 0
4 2
dtype: int64
b = mask.any(1).sum()
print (b)
4