簡體   English   中英

Python:計算從csv文件導入的兩列的差,並存儲到python腳本中的另一列

[英]Python : Calculate the difference of two columns imported from a csv file and store to another column in python script

我已經在python程序中導入了一個.csv文件,其中使用pandas模塊包含了許多列。 在我的代碼中,我只是導入了前三列。 代碼和示例文件如下。

import pandas as pd
fields = ['TEST ONE', 'TEST TWO', 'TEST THREE']
df1=pd.read_csv('List.csv', skipinitialspace=True, usecols=fields)

樣本文件


如何在我的python程序中找到列TEST ONETEST TWO 2的區別,並將其存儲在代碼內的單獨位置/列/數組中,以便可以在需要時從中提取值。 我想找到作為前兩列之差而生成的新列的均值和最大值。

做這樣的事情。

df1['diff'] =  df1['TEST ONE'] - df1['TEST TWO']
#The Dataframe would be df1 throughout
# This will store it as a column of that same dataframe.
# When you need the difference, use that column just like normal pandas column.
mean_of_diff = df1['diff'].mean()
max_of_diff = df1['diff'].max()
# For third value of difference use the third index of dataframe
third_diff = df1.loc[2, 'diff']

注意:我使用2作為從0開始的索引。索引也可以是字符串或日期。 傳遞適當的索引值以獲得所需的結果。

Difference = df1['TEST ONE'] - df['TEST TWO']

區別將是熊貓系列。 在那你可以使用均值和最大值

Difference.mean()
Difference.max() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM