我在字典里有一个.csv文件,里面有一些数据。我想做的是迭代dataframe(它本身在dictionary中)中的特定列(使用字符串),并根据条件在该行(但在新列)中指定特定的数字
import os
from os import listdir
from os.path import isfile, join
import pandas as pd
### INPUT DIRECTORY
path="folder"
### READING .csv FILES TO THE "dictionary"
files=[f.split('.')[0] for f in listdir(path) if isfile(join(path, f))]
dictionary={}
for file in files:
dictionary[file]=pd.read_csv(path+'/'+file+'.csv')
### DROPPING 2ND ROW
results={}
for df in dictionary:
results[str(df)+'_CONSTANT_VAR'] = dictionary[df]
results[str(df)+'_CONSTANT_VAR'] = results[str(df)+'_CONSTANT_D_SHALE_VAR'].iloc[1:]
for df in results:
for i in results[str(df)]['FORMATION']:
if i=='BAL6':
results[str(df)]['VAR'][i]=10 ### HERE I WANT TO ADD VALUE TO THE NEW COLUMN
不幸的是,代码只是将“10”放在所有地方,而不仅仅放在满足条件的行上。 知道为什么会这样吗?如何按照我想要的方式去做
此外,还会弹出一个错误:
<input>:27: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead
输入数据:
wellName DEPTH FORMATION depth2
well name 1000 bal0.5 123
well name 2000 bal1 124
well name 3000 bal0.6 125
well name 4000 bal2 126
well name 5000 bal0.7 127
well name 6000 bal3 128
well name 7000 bal0.8 129
well name 8000 bal4 130
well name 9000 bal0.9 131
well name 10000 bal5 132
well name 11000 bal0.10 133
well name 12000 bal6 134
well name 13000 bal0.11 135
输出IAM获取:
wellName DEPTH FORMATION depth2 VAR
well name 1000 bal0.5 123 10
well name 2000 bal1 124 10
well name 3000 bal0.6 125 10
well name 4000 bal2 126 10
well name 5000 bal0.7 127 10
well name 6000 bal3 128 10
well name 7000 bal0.8 129 10
well name 8000 bal4 130 10
well name 9000 bal0.9 131 10
well name 10000 bal5 132 10
well name 11000 bal0.10 133 10
well name 12000 bal6 134 10
well name 13000 bal0.11 135 10
我想要的输出:
wellName DEPTH FORMATION depth2 VAR
well name 1000 bal0.5 123
well name 2000 bal1 124
well name 3000 bal0.6 125
well name 4000 bal2 126
well name 5000 bal0.7 127
well name 6000 bal3 128
well name 7000 bal0.8 129
well name 8000 bal4 130
well name 9000 bal0.9 131
well name 10000 bal5 132
well name 11000 bal0.10 133
well name 12000 bal6 134 10 ### VALUE ADDED ONLY HERE
well name 13000 bal0.11 135
给定数据帧
df
(如输入数据中所示),您可以使用以下命令有条件地指定一个新列VAR
或在列VAR
中指定一个值您得到的“error”消息实际上是一个警告,您为数据帧的副本指定了一个新值,并且数据帧本身不会被更改。这称为链式索引并解释为here
相关问题 更多 >
编程相关推荐