Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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_Pandas_Dataframe - Fatal编程技术网

Python 为数据帧中的单元格赋值

Python 为数据帧中的单元格赋值,python,pandas,dataframe,Python,Pandas,Dataframe,我正在遍历该文件,并希望根据索引和列设置DataFrame中单元格的值 f = "D:/Technical_Data/1pep.csv" df = pd.read_csv(f, header=0, sep=',') save_file = "D:/Result_of_TA/" + "def.csv" qbfile = open(save_file,"r") for aline in qbfile.readlines(): values = aline.split(",") if

我正在遍历该文件,并希望根据索引和列设置
DataFrame
中单元格的值

f = "D:/Technical_Data/1pep.csv"
df = pd.read_csv(f, header=0, sep=',')
save_file = "D:/Result_of_TA/" + "def.csv"
qbfile = open(save_file,"r")
for aline in qbfile.readlines():
    values = aline.split(",")
    if values[58].strip()=='BUY' :
       no_of_shares = price/float(values[4])
    if values[58].strip()=='SELL' :
        price = no_of_shares * float(values[4]) 
    df.ix[values[0],'Price'] = price
qbfile.close()
df.to_csv(save_file)
我正在犯错误

  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:3979)
  File "pandas\index.pyx", line 152, in pandas.index.IndexEngine.get_loc (pandas\index.c:3782)
  File "pandas\index.pyx", line 178, in pandas.index.IndexEngine._get_loc_duplicates (pandas\index.c:4213)
  File "pandas\index.pyx", line 385, in pandas.index.Int64Engine._maybe_get_bool_indexer (pandas\index.c:7683)
KeyError: '20150101'

两个文件中的列0都是索引

我建议使用
pandas
读取两个
csv
文件,而不是循环通过
save\u文件
。然后,您可以创建各种索引方案和计算,将数据从
qb
移动到
1pep
。您可以这样构造代码

import pandas as pd

# File locations
pep1_file = 'D:/Technical_Data/1pep.csv'
qb_file = 'D:/Result_of_TA/def.csv.csv'

# Read csv files
pep1 = pd.read_csv(pep1_file, header=0, sep=',')
qb = pd.read_csv(qb_file, header=0, sep=',')

# Process data    
# Find rows with buy in 58th col?
buy_index = qb.iloc[:, 58].str.contains('BUY')

# Find rows with sell in 58th col?
sell_index = qb.iloc[:, 58].str.contains('SELL')

# Get something from 4th col in buy rows?
something_from_purchase = qb.ix[buy_index, 4].astype(float)

# Derive number of shares 
# Where do you get your initial price from?
# It is used before it is assigned if buy row happens first in the original code?
no_of_shares = price / something_from_purchase

# Get something from 4th col in sell rows?
something_from_sale = qb.ix[sell_index, 4].astype(float)

# Derive price
# Where do you get your initial no_of_shares from?
# It is used before it is assigned if sell row happens first in the original code?
price = no_of_shares * something_from_sale

# Assign pep1 price based on qb index
pep1.loc[qb.index, 'Price'] = price

# Then write csv file
# Are sure you want to overwrite?
pep1.to_csv(qb_file)

现在你的代码不清楚。有一些初始值依赖性(
无股票
价格
)似乎是循环的,一些计算可能是反向的。如果没有一些示例数据或对结构的解释,就无法推荐具体的代码。

您可以发布几行数据吗?您真的想覆盖
保存文件吗?