相关疑难解决方法(0)

如何在Pandas中处理SettingWithCopyWarning?

背景

我刚刚将我的Pandas从0.11升级到0.13.0rc1.现在,该应用程序正在弹出许多新的警告.其中一个是这样的:

E:\FinReporter\FM_EXT.py:449: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
  quote_df['TVol']   = quote_df['TVol']/TVOL_SCALE
Run Code Online (Sandbox Code Playgroud)

我想知道究竟是什么意思?我需要改变什么吗?

如果我坚持使用,我应该如何暂停警告quote_df['TVol'] = quote_df['TVol']/TVOL_SCALE

给出错误的函数

def _decode_stock_quote(list_of_150_stk_str):
    """decode the webpage and return dataframe"""

    from cStringIO import StringIO

    str_of_all = "".join(list_of_150_stk_str)

    quote_df = pd.read_csv(StringIO(str_of_all), sep=',', names=list('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefg')) #dtype={'A': object, 'B': object, 'C': np.float64}
    quote_df.rename(columns={'A':'STK', 'B':'TOpen', 'C':'TPCLOSE', 'D':'TPrice', 'E':'THigh', 'F':'TLow', 'I':'TVol', 'J':'TAmt', 'e':'TDate', 'f':'TTime'}, inplace=True)
    quote_df = quote_df.ix[:,[0,3,2,1,4,5,8,9,30,31]]
    quote_df['TClose'] = quote_df['TPrice']
    quote_df['RT'] …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas chained-assignment

536
推荐指数
16
解决办法
58万
查看次数

Pandas将字符串转换为int

我有一个带ID号的大型数据框:

ID.head()
Out[64]: 
0    4806105017087
1    4806105017087
2    4806105017087
3    4901295030089
4    4901295030089
Run Code Online (Sandbox Code Playgroud)

这些都是目前的所有字符串.

我想转换为int不使用循环 - 为此我使用ID.astype(int).

问题是我的一些行包含无法转换为的脏数据int,例如

ID[154382]
Out[58]: 'CN414149'
Run Code Online (Sandbox Code Playgroud)

我怎样才能(不使用循环)删除这些类型的事件,以便我可以astype安心使用?

python pandas

28
推荐指数
2
解决办法
9万
查看次数

标签 统计

pandas ×2

python ×2

chained-assignment ×1

dataframe ×1