我有一个列表理解,在其中,我调用一个函数,并向其传递两个参数。在该函数中,我还有另一个列表理解功能,可以给我一个DataFrames列表。
我必须清理每个DataFrame中的数据,因此我使用了一个for循环来遍历列表中的每个DataFrame。在每次迭代中,我都会做我需要做的事情,其中之一就是重置每个DataFrame的索引。我在函数外放置了一条print语句,以确保可以按照所需的方式获取所有内容,但不会重置索引。为什么不重置?
def function(xls, a_list):
# a_list is a list of strings
df_list = [pd.read_excel(xls, sheet_name=a) for a in a_list]
for df in df_list:
df.dropna(how='all', inplace=True)
df['Meal'] = df['Meal'].fillna(method='ffill')
# RIGHT HERE
df = df.reset_index(drop=True)
return df_list
# ------------------------------------
list_of_df = [function(xls, monthly_sheets) for xls, monthly_sheets in zip(xls_files, sheet_names) if monthly_sheets]
作为一个例子,这就是我得到的:
Col1 Col2
0 a f
1 b g
4 c h
7 d i
8 e j
我想要的是这个
Col1 Col2
0 a f
1 b g
2 c h
3 d i
4 e j
我想念什么?
谢谢!
答案 0 :(得分:1)
代替
[root@localhost serving]# tools/bazel_in_docker.sh bazel build --config=nativeopt tensorflow_serving/...
== Pulling docker image: tensorflow/serving:nightly-devel
Trying to pull repository docker.io/tensorflow/serving ...
nightly-devel: Pulling from docker.io/tensorflow/serving
Digest: sha256:f500ae4ab367cbabfd474487175bb357d73c01466a80c699db90ba3f0ba7b5a8
Status: Image is up to date for docker.io/tensorflow/serving:nightly-devel
== Running cmd: sh -c 'cd /root/serving; TEST_TMPDIR=.cache bazel build --config=nativeopt tensorflow_serving/...'
usermod: no changes
$TEST_TMPDIR defined: output root default is '/root/serving/.cache' and max_idle_secs default is '15'.
Starting local Bazel server and connecting to it...
.............
ERROR: error loading package '': Encountered error while reading extension file 'tensorflow/workspace.bzl': no such package '@org_tensorflow//tensorflow': /root/serving/.cache/_bazel_root/01a289b7faaf5ec651fb0e4e35f862a1/external/org_tensorflow must be an existing directory
ERROR: error loading package '': Encountered error while reading extension file 'tensorflow/workspace.bzl': no such package '@org_tensorflow//tensorflow': /root/serving/.cache/_bazel_root/01a289b7faaf5ec651fb0e4e35f862a1/external/org_tensorflow must be an existing directory
INFO: Elapsed time: 0.460s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
使用
df = df.reset_index(drop=True)
问题在于,如果df.reset_index(drop=True, inplace=True)
为df.reset_index()
,inplace
返回一个值,并且您已将该值分配为False
,但是您df
不会做任何进一步的事情。您正在使用数据框列表,并且该列表不包含您刚刚创建的新的本地 df
。
另一种选择是将新的df
存储在您的df
中。尽管对我来说,这感觉还需要更多工作,因为熊猫已经为大多数操作提供了就地论证。