我使用以下代码将数据保存到cvs文件中:
filename='shareListing.csv'
allshares=pd.read_csv(filename)
i=0
for item in allshares['Code']:
try:
print item
ebitdata=fdata(item)
allshares.loc[:,('endDate')][i]=ebitdata[0]
allshares.loc[:,('EBIT')][i] = ebitdata[1]
print ebitdata[1]
except Exception, e:
allshares.loc[:,('EBIT')][i] = ['NA']
allshares.to_csv('results.csv')
shareListing.csv看起来如下(endDate和EBIT列为空):
代码shortName endDate
000001.XSHE平安银行
000002.XSHE万科A
000003.XSHE金田A
000004.XSHE国农科技
000005.XSHE世纪星源
000006.XSHE深振业A
还有一个名为fdata的函数,它为每个特定项返回"endDate“和"EBIT”。
在执行时,我得到了以下信息:
000001.XSHE
E:/Python/py27/DataYes/Prices.py:91: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
allshares.loc[:,('EBIT')][i] = ['NA']
000002.XSHE
C:\Users\ly\python27\lib\site-packages\pandas\core\indexing.py:179: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)
E:/Python/py27/DataYes/Prices.py:85: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
请参阅文档中的注意事项:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
allshares.loc[:,('endDate')][i]=ebitdata[0]
20901592186.7
000003.XSHE
我遵循文档并使用.loc
更改了索引函数,但是仍然是相同的消息,而results.csv文件只在endDate
和EBIT
列中填充了第一个条目,其余的都是空的。
请帮帮忙。
发布于 2017-11-20 07:55:21
我认为你需要改变:
allshares.loc[:,('endDate')][i]=ebitdata[0]
allshares.loc[:,('EBIT')][i] = ebitdata[1]
allshares.loc[:,('EBIT')][i] = ['NA']
至:
allshares.loc[i,'endDate'] = ebitdata[0]
allshares.loc[i,'EBIT'] = ebitdata[1]
allshares.loc[i,'EBIT'] = 'NA'
或者如果需要丢失值NaN
allshares.loc[i,'EBIT'] = np.nan
更好的解决方案是在函数中捕获Exception
并返回Series
,然后使用apply
for循环:
allshares = pd.DataFrame({'Code':list('abcdef'),
'B':[4,5,4,5,5,4]})
def fdata(x):
try:
#rewrite to your code
if x != 'e':
#return Series instead tuple
return pd.Series([x * 2, x * 3])
else:
#some sample exception for e value
allshares['not exist']
except Exception as e:
#return Series instead tuple
return pd.Series(['NA', 'NA'])
#for NaNs
#return pd.Series([np.nan, np.nan])
allshares[['endDate','EBIT']] = allshares['Code'].apply(fdata)
print (allshares)
B Code endDate EBIT
0 4 a aa aaa
1 5 b bb bbb
2 4 c cc ccc
3 5 d dd ddd
4 5 e NA NA
5 4 f ff fff
但是,如果change函数fdata
为阴极异常和返回序列创建了另一个函数:
def fdata(x):
if x != 'e':
return (x * 2, x * 3)
else:
return allshares['not exist']
def func(x):
try:
return pd.Series(fdata(x))
except Exception as e:
return pd.Series(['NA', 'NA'])
#return pd.Series([np.nan, np.nan])
allshares[['endDate','EBIT']] = allshares['Code'].apply(func)
print (allshares)
B Code endDate EBIT
0 4 a aa aaa
1 5 b bb bbb
2 4 c cc ccc
3 5 d dd ddd
4 5 e NA NA
5 4 f ff fff
https://stackoverflow.com/questions/47387303
复制相似问题