我试图从某一列中减去选定的列,但它给了我一个错误"ValueError:应为1D数组,得到的是形状为"(3,4("的数组;。我的csv文件如下所示:
Number Nimi Summa Kino
0 1 Jan 30.5 12.5
1 2 Pets 30.5 12.5
2 3 Anni 30.5 12.5
因为用户可以创建无限数量的列,所以我需要选择除"列"之外的每一列;数字,尼米和suma";。结果应该是"结果"之后的每一列;Summa";需要从";Summa";。我希望这是有道理的。
df = pd.read_csv ('eelarve.csv')
columns_dont_want = ["Number", "Nimi", "Summa"]
select = [x for x in df.columns if x not in columns_dont_want]
df["Summa järel"] = df["Summa"] - df.loc[:, select]
df.to_csv('eelarve.csv', index=False)
我在df中添加了更多带有数据的列,以确保下面的代码能够工作。
import pandas as pd
df = pd.DataFrame({'Number': [1, 2, 3],
'Nimi': ['Jan', 'Pets', 'Anni'],
'Summa': [30.5, 30.5, 30.5],
'Kino': [12.5, 12.5, 12.5],
'Test1': [0, 1, 2],
'Test2': [0.1, 0.2, 0.3]
})
columns_dont_want = ["Number", "Nimi", "Summa"]
select = [x for x in df.columns if x not in columns_dont_want]
df['Summa järel'] = df['Summa'] - df[select].sum(axis=1)
输出为:
Number Nimi Summa Kino Test1 Test2 Summa järel
0 1 Jan 30.5 12.5 0 0.1 17.9
1 2 Pets 30.5 12.5 1 0.2 16.8
2 3 Anni 30.5 12.5 2 0.3 15.7
您可以执行drop
+rsub
:
df.drop(columns=columns_dont_want).rsub(df['Summa'], axis=0)
输出:
Kino
0 18.0
1 18.0
2 18.0
加入其他列:
out = (df[columns_dont_want]
.join(df.drop(columns=columns_dont_want)
.rsub(df['Summa'], axis=0))
)
输出:
Number Nimi Summa Kino
0 1 Jan 30.5 18.0
1 2 Pets 30.5 18.0
2 3 Anni 30.5 18.0