[英]Multiplication on slice of pandas dataframe
I have a dataframe like this: 我有一个这样的数据框:
Year A_annex Arnston Bachelor Berg
1955 1.625 0.940 NaN NaN
1956 1.219 1.018 NaN NaN
1957 2.090 1.20 NaN 1.190
1958 0.950 1.345 NaN 1.090
and I want to multiply everything in [1:,1:]
by .404 我想将[1:,1:]
的所有内容乘以.404
The code I am trying is: 我正在尝试的代码是:
df=pd.read_csv(r'H:\Sheyenne\Grazing Records\Master_AMU_complete.csv')
hectare=0.404
df=df.iloc[1:,1:]
df=df*hectare
but this returns: 但这返回:
TypeError: Could not operate 0.404686 with block values can't multiply sequence by non-int of type 'float'
printing df.info()
says everything after the slice is a non-null object if that helps. 打印df.info()
表示切片后的所有内容都是非null对象(如果有帮助的话)。
Yes, it is problematic value. 是的,这是有问题的价值。 You can find these problematic values by function (thanks ajcr): 您可以按功能找到这些有问题的值(感谢ajcr):
df = df.convert_objects(convert_numeric=True)
First NaN
are converted to 0
, then apply function above and it return NaN instead of problematic values. 首先将NaN
转换为0
,然后在上方应用函数,然后返回NaN而不是有问题的值。 So you have to find rows with NaN values and return subset of original df
. 因此,您必须找到具有NaN值的行并返回原始df
子集。
print df
Year A_annex Arnston Bachelor Berg
0 1955 1.625 0.940 NaN NaN
1 1956 1.219 1.018 NaN NaN
2 1957 2.090 1.20a NaN 1.19
3 1958 0.950 1.345a NaN 1.09
test = df.fillna(0)
test = test.convert_objects(convert_numeric=True)
Year A_annex Arnston Bachelor Berg
0 1955 1.625 0.940 0 0.00
1 1956 1.219 1.018 0 0.00
2 1957 2.090 NaN 0 1.19
3 1958 0.950 NaN 0 1.09
test = df[test.isnull().any(axis=1)]
Year A_annex Arnston Bachelor Berg
2 1957 2.09 1.20a NaN 1.19
3 1958 0.95 1.345a NaN 1.09
hectare=0.404
df=df.iloc[1:,1:]
df=df*hectare
print df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.