数据帧写入cvs失败

时间:2017-11-20 07:52:57

标签: python dataframe

我使用以下代码将数据保存到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 EBIT

000001.XSHE平安银行
000002.XSHE万科A
000003.XSHE PT金田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文件只在endDateEBIT列中填充了第一个项目,其余的都是空白的。

请帮忙。

1 个答案:

答案 0 :(得分:2)

我认为你需要改变:

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进行循环:

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

但是如果canot更改函数fdata为cathc异常创建另一个并返回Series:

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