UserWarning:Pandas不允许通过新的属性名称创建列 [英] UserWarning: Pandas doesn't allow columns to be created via a new attribute name
问题描述
我迷上了熊猫脚本。
实际上,我正在处理两个csv文件(一个输入,另一个输出文件)。
i要复制两列的所有行并要进行计算,然后将其复制到另一个数据框(输出文件)。
Actually , i am working with two csv file(one input and the other output file). i want to copy all the rows of two column and want to make calculation and then copy it to another dataframe (output file).
这些列如下:
'lat', 'long','PHCount', 'latOffset_1', 'longOffset_1','PH_Lat_1', 'PH_Long_1', 'latOffset_2', 'longOffset_2', 'PH_Lat_2', 'PH_Long_2', 'latOffset_3', 'longOffset_3','PH_Lat_3', 'PH_Long_3', 'latOffset_4', 'longOffset_4','PH_Lat_4', 'PH_Long_4'.
我想取列 lat和 latOffset_1,进行一些计算并将其放入我已经创建的另一个新列('PH_Lat_1')。
i want to take the column 'lat' and 'latOffset_1' , do some calculation and put it in another new column('PH_Lat_1') which i have already created.
我的功能是:
def calculate_latoffset(latoffset): #Calculating Lat offset.
a=(df2['lat']-(2*latoffset))
return a
主要代码:
for i in range(1,5):
print(i)
a='PH_lat_%d' % i
print (a)
b='latOffset_%d' % i
print (b)
df2.a = df2.apply(lambda x: calculate_latoffset(x[b]), axis=1)
由于列名只是相差(1,2,3,4)。所以我想一次调用函数calculate_latoffset并计算所有列的所有行(PH_Lat_1,PH_Lat_2,PH_Lat_3,PH_Lat_4)。
Since the column name just differ by (1,2,3,4). so i want to call the function calculate_latoffset and calculate the all the rows of all the columns(PH_Lat_1, PH_Lat_2, PH_Lat_3,PH_Lat_4) in one go.
在使用上述方法时代码我遇到此错误:
When using the above code i am getting this error :
basic_conversion.py:46: UserWarning: Pandas doesn't allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
df2.a = df2.apply(lambda x: calculate_latoffset(x[b]), axis=1)
有可能吗?
请帮助
is it possible ? Please kindly help
推荐答案
只需使用 df2 ['a']
而不是 df2.a
这篇关于UserWarning:Pandas不允许通过新的属性名称创建列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!