Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在字典中的数据帧迭代期间创建和填充列_Python_Dataframe_Dictionary - Fatal编程技术网

Python 如何在字典中的数据帧迭代期间创建和填充列

Python 如何在字典中的数据帧迭代期间创建和填充列,python,dataframe,dictionary,Python,Dataframe,Dictionary,字典里有一个.csv文件,里面有一些数据。我想做的是在dataframe(它本身在字典中)中的特定列(带字符串)上迭代,并根据条件在该行(但在新列中)分配特定的数字 导入操作系统 从操作系统导入listdir 从os.path导入isfile,加入 作为pd进口熊猫 ###输入目录 path=“文件夹” ###正在将.csv文件读取到“字典” files=[f.split('.')[0]表示listdir(路径)中的f,如果isfile(join(path,f))] 字典={} 对于文件中的文件

字典里有一个.csv文件,里面有一些数据。我想做的是在dataframe(它本身在字典中)中的特定列(带字符串)上迭代,并根据条件在该行(但在新列中)分配特定的数字

导入操作系统
从操作系统导入listdir
从os.path导入isfile,加入
作为pd进口熊猫
###输入目录
path=“文件夹”
###正在将.csv文件读取到“字典”
files=[f.split('.')[0]表示listdir(路径)中的f,如果isfile(join(path,f))]
字典={}
对于文件中的文件:
字典[文件]=pd.read_csv(路径+'/'+文件+'.csv')
###下降第二排
结果={}
对于字典中的df:
结果[str(df)+'''u常量\u变量']=字典[df]
结果[str(df)+“U常量”变量]=结果[str(df)+“U常量”变量].iloc[1:]
对于结果中的df:
对于结果[str(df)][‘形成’]中的i:
如果i=='BAL6':
结果[str(df)]['VAR'][i]=10#####这里我想为新列添加值
不幸的是,代码只是将“10”放在所有地方,而不仅仅放在满足条件的行上。 知道为什么会这样吗?我想怎么做


此外,还会弹出一个错误:

:27:设置为带有CopyWarning:
试图在数据帧切片的副本上设置值。
尝试改用.loc[row\u indexer,col\u indexer]=value

输入数据:

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
中分配一个值

df.loc[(df.FORMATION == 'bal6'), 'VAR'] = 10

您收到的“错误”消息实际上是一个警告,即您为数据帧的副本指定了一个新值,并且数据帧本身不会更改。这被称为链式索引并已解释。

能否添加一个示例,说明您的数据外观、您获得的输出以及您期望的输出?感谢您对错误的解释。下面是我修改代码后得到的结果:回溯(最近一次调用last):文件“”,在AttributeError中的第24行:“str”对象没有属性“loc”,我说的是franky,我不明白为什么需要这些字典的东西。只需在数据帧上执行所有数据操作。完成后,如果to_dict()需要,您可以将它们转换为字典
str(df)
将在每次更改df时更改,因此每次都会得到新的字典键。使用字典的原因是我将有数百个这样的数据集,不确定您尝试了什么,但它应该是:
对于结果中的df:
并且在下一行缩进
results[df].loc[(results[df].FORMATION='bal6'),'VAR']=10
效果很好,csv中有一个错误,就是第一次没有正常运行。谢谢