我想解决两个问题。一个是你如何获得你的 CSV,另一个是你的代码中可能不起作用的东西。
如何获取关联值
您尝试做的是该shift()方法的一项不错的任务
# you probably don't need the semicolons at the end of the line, right?
# if you want to get rid of them, you can do:
df= pd.read_csv(
r"CSVFILEPATH", # your file
engine='python', # use the python engine instead of C to use a regex as separator
sep=r'[,;]', # use ; as an alternative separator
usecols=range(3), # exclude the last column (after the ;)
names=range(3)) # assign names, if you like you can also assign a list of more verbose column names here (this just uses numbers)
# create a dataframe that is a version of the original
# which is just one row shifted to the top
df_shifted= df.shift(-1)
# concatenate it with the original data frame and assign unique column names
df_concat= pd.concat([df, df_shifted], axis='columns')
df_concat.columns= range(6)
# apply your filter
newdf= df.loc[df_concat[0].str.contains('LLRT|LLTX|LRRT|LTRC|LV10|LV3|LEDR|LTRV|LES2|LES1', regex = True)]
这输出:
In [44]: newdf
Out[44]:
0 1 2 3 4 5
0 OBSERV\LTRC CL1 0.0 OBVAL\14 NaN 0.0
2 OBSERV\LTRC CL1 10.0 OBVAL\14 NaN 0.0
4 OBSERV\LTRC CL1 20.0 OBVAL\14 NaN 0.0
6 OBSERV\LTRC CL1 30.0 OBVAL\14 NaN 0.5
根据您提供的以下测试数据:
import io
import pandas as pd
raw=\
r"""OBSERV\LTRC,CL1,0,10.00;
OBVAL\14,,0,V;
OBSERV\LTRC,CL1,10,20.00;
OBVAL\14,,0,V;
OBSERV\LTRC,CL1,20,30.00;
OBVAL\14,,0,V;
OBSERV\LTRC,CL1,30,40.00;
OBVAL\14,,0.5,V;"""
df= pd.read_csv(io.StringIO(raw), engine='python', sep=r'[,;]', usecols=range(3), names=range(3))
代码,那可能不行,你打算做什么
# the following line references a method, but doesn't call it:
df.set_index
# if you execute this line, it outputs:
df.set_index
Out[18]:
<bound method DataFrame.set_index of 0 1 2
0 OBSERV\LTRC CL1 0.0
1 OBVAL\14 NaN 0.0
...
# look at the <bound method part, this is the __repr__ string
# of the object (bound methods are objects themselfes)
# if you execute this line inbetween a script, it has no effect
# at all (just maybe slows down execution a very tiny bit)
# because you don't do anything with the returned object
你写了,你得到了消息generator object <genexpr> at 0x0000028057C33648>]。这不是错误方法。和bound method上面的消息一样,这也是一个对象的__repr__字符串。在这种情况下,一个生成器对象。如果您调用append一个列表,它会将传入的参数append视为一个对象。我猜您宁愿将增加一的索引添加到现有列表中。这可以通过以下代码完成:
# create a copy of the list to avoid funny results
new_indices=list(observ_ind)
# add the elements of returned by the generator object
# to the list (rather than the generator object itself
new_indices.extend(i + 1 for i in observ_ind)