【问题标题】:Error in pandas while subtracting two values and storing them again减去两个值并再次存储它们时熊猫出错
【发布时间】:2015-11-08 17:39:42
【问题描述】:
First.csv
LAC Reference_Count
1000    500
2222    1000
3333    500
5555    1000
9999    1500

Second.csv
LAC 10/08/15 00:00  10/08/15 01:00
1000    2000    2500
2222    3000    4000

我有两个文件 first.csv 和 second.csv,在 first.csv 我有两个标题 LAC 和引用计数,second.csv 没有标题可以是任何数字,但它将遵循一种格式,即一列LAC 所有 id,后跟时间日期序列,我需要从 second.csv 中获取 LAC(id),并在 first.csv 的 LAC 中搜索,获取引用计数并减去 second.csv 的所有时间序列的值。您可以通过预期输出更好地理解。

second.csv 
    LAC  10/08/15 00:00  10/08/15 01:00
0  1000            1500            2000
1  2222            2000            3000

这是我的代码

import pandas as pd
location='/home/saiharsh/Documents/Crowd Street/BUgs/second.py.csv'
master_lac_path='/home/saiharsh/Documents/Crowd Street/Final/first.csv'
master_csv_file = master_lac_path
df_master=pd.read_csv(master_csv_file,error_bad_lines=False)
df2=pd.read_csv(location,error_bad_lines=False)
header=list(df2.iloc[:,1:].columns.values)
print df2
for i in range(len(df2)):
            val=df_master[df_master['LAC']==df2['LAC'][i]]['Reference_Count']
            for j in header:
                Tmp=df2[j][i]
                tmp=df2[df2[j]==Tmp][j]
                value=tmp-val
                print type(tmp),type(val),type(value)
                df2.set_value(i,j,value)
print df2

我收到了这个错误

Traceback (most recent call last):
  File "/home/Py_Process_Plots/master_lac.py", line 14, in process
    value=tmp-val
  File "/home/software/anaconda/lib/python2.7/site-packages/pandas/core/ops.py", line 524, in wrapper
    arr = na_op(lvalues, rvalues)
  File "/home/software/anaconda/lib/python2.7/site-packages/pandas/core/ops.py", line 475, in na_op
    result[mask] = op(x[mask], _values_from_object(y[mask]))
TypeError: unsupported operand type(s) for -: 'str' and 'float'

我的 pandas 版本是 0.16.2,numpy 是 1.9.2

请帮我解决问题。

【问题讨论】:

    标签: python csv numpy pandas


    【解决方案1】:

    尝试将所有内容都转换为浮点数,因为错误表示您有一个字符串,您正在尝试使用它进行数学运算。

    你试过了吗:

    float(value) = float(tmp) - float(val)
    

    如果之后错误发生变化,请告诉我。也发布错误。

    【讨论】:

    • 任何解决方案。它尝试了 int() 但不起作用
    • 即使我已经尝试过了,错误是Traceback (most recent call last): File "main.py", line 55, in <module> process() File "main.py", line 46, in process master_lac.process(tmp,master_lac_path) File "/home/Py_Process_Plots/master_lac.py", line 18, in process tmp=float(tmp) File "/home/software/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 77, in wrapper "cannot convert the series to {0}".format(str(converter))) TypeError: cannot convert the series to <type 'float'>
    【解决方案2】:

    这行得通:

    df1 = pd.read_csv('First.csv', sep='\t', header=0, index_col=0)
    df2 = pd.read_csv('Second.csv', sep='\t', header=0, index_col=0)
    dfans = df2.subtract(df1.iloc[:,0], axis=0).dropna()
    
    print(df1)
          Reference_Count
    LAC                  
    1000              500
    2222             1000
    3333              500
    5555             1000
    9999             1500
    
    print(df2)
          10/08/15 00:00  10/08/15 01:00
    LAC                                 
    1000            2000            2500
    2222            3000            4000
    
    print(dfans)
          10/08/15 00:00  10/08/15 01:00
    LAC                                 
    1000            1500            2000
    2222            2000            3000
    

    您需要将 LAC 作为索引。以下不起作用...

    index_default = pd.read_csv('test.csv', sep='\t', header=0)
    
    print(index_default)
        LAC  10/08/15 00:00  10/08/15 01:00
    0  2222            3000            4000
    1  1000            2000            2500
    
    dfans = index_default.subtract(df1.iloc[:,0], axis=0).dropna()
    
    print(dfans)
    Empty DataFrame
    Columns: [LAC, 10/08/15 00:00, 10/08/15 01:00]
    Index: []
    

    但是以 LAC 作为索引,它可以工作...

    index_lac = pd.read_csv('test.csv', sep='\t', header=0, index_col=0)
    
    print(index_lac)
          10/08/15 00:00  10/08/15 01:00
    LAC                                 
    2222            3000            4000
    1000            2000            2500
    
    dfans = index_lac.subtract(df1.iloc[:,0], axis=0).dropna()
    
    print(dfans)
          10/08/15 00:00  10/08/15 01:00
    LAC                                 
    1000            1500            2000
    2222            2000            3000
    

    【讨论】:

    • 输入时不工作 LAC 10/08/15 00:00 10/08/15 01:00 0 2222 3000 4000 1 1000 2000 2500
    • 在您的问题中,您让 pandas 为您创建索引。在您的评论中,您还让 pandas 为您创建索引。如果您使用 'index_col=0' 代替,那么 LAC 将成为您的索引,然后它应该可以工作。
    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 2016-06-21
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多