我需要通过执行值的简单算术组合来使用现有列创建新列。这是我的代码。
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
class Dataset():
def __init__(self, input):
self.choice = input
self.file = 0
def read(self):
if self.choice == ("1"):
self.file = pd.read_csv('1')
self.file.plot(kind='bar')
print(df)
i = (input("Pick a DataSet= "))
df = Dataset(i)
df.read()
plt.show()
我尝试过这些建议:
#'Dataset' object does not support item assignment --> df['New Column'] = ['Reference Area'] + ['Ratio']
#TypeError: 'Dataset' object is not subscriptable -> df['NEW COL'] = df[['Reference Area', 'Ratio']].sum(axis=1)
答案 0 :(得分:0)
变量df
的类型为DataSet
,而非pandas
。 pandas
变量位于DataSet类(变量self.file
)内,因此您应该能够执行以下操作:
df.file['New Column'] = df.file['Reference Area'] + df.file['Ratio']
来自课外。或者,您可以向类中添加一个方法来执行此操作:
def create_new_column(self):
self.file['New Column'] = self.file['Reference Area'] + self.file['Ratio']