pandas 为什么我的熊猫数据框变成了“无”类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42478887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my pandas dataframe turning into 'None' type?
提问by durbachit
I can't see where is my problem, this code worked ok for a very simple example, but when I started working with my real data, I encountered problems.
我看不出我的问题出在哪里,这个代码对于一个非常简单的例子来说工作正常,但是当我开始使用我的真实数据时,我遇到了问题。
I am basically extracting certain data from various csv files and trying to eventually combine them in a single dataframe.
我基本上是从各种 csv 文件中提取某些数据,并试图最终将它们组合在一个数据框中。
The relevant piece of code is below:
相关代码如下:
wavenames = ['W1_', 'W2_', 'W3_']
logs=['log1','log2','log3','log4']
for w in wavenames:
AllSynt = pd.DataFrame(index=range(6341), columns=['X']+logs)
AllSynt['X']=z # an array extracted from elsewhere
print AllSynt.head(3)
for f in files:
for l in logs:
if (f.startswith('Synthetic_'+w)) & (f.endswith(l+'.csv')):
df = pd.read_csv(path+f,delimiter=',')
AllSynt = pd.DataFrame(AllSynt)
AllSynt = AllSynt.merge(df,how='left',on='X')
AllSynt = AllSynt.rename(columns = {'Y':l}, inplace = True)
print '\n', AllSynt.head(5)
but this gives me AttributeError: 'NoneType' object has no attribute 'head'
(before making sure that the AllSynt is a pandas dataframe at the beginning of the loop, I got the same error (just saying it has no attribute 'merge'). Why is my AllSynt dataframe permanently turning into a None
?
但这给了我AttributeError: 'NoneType' object has no attribute 'head'
(在循环开始时确保 AllSynt 是一个 Pandas 数据帧之前,我得到了同样的错误(只是说它没有属性“合并”)。为什么我的 AllSynt 数据帧永久变成了None
?
采纳答案by Nickil Maveli
You would need to rewrite the line :
您需要重写该行:
AllSynt = AllSynt.rename(columns={'Y':l}, inplace=True)
AllSynt = AllSynt.rename(columns={'Y':l}, inplace=True)
to simply the following:
简单地说:
AllSynt.rename(columns={'Y':l}, inplace=True) # No assigning with inplace parameter
# (or)
AllSynt = AllSynt.rename(columns={'Y':l}) # assign without inplace parameter
When you specify inplace=True
and want to see it's contents, it would return None
as they merely mutate the DF
instead of creating a new copy of it. Basically, you're assigning None
to the result and hence it complains of the AttributeError
as it isn't a pd.DataFrame
object anymore to access it's .head()
method.
当您指定inplace=True
并想要查看它的内容时,它会返回,None
因为它们只是改变DF
而不是创建它的新副本。基本上,您正在分配None
结果,因此它会抱怨 ,AttributeError
因为它pd.DataFrame
不再是访问它的.head()
方法的对象。
Similar analogy could be observed by doing list.append()
, list.sort()
etc operations in pure python while assigning their results to a variable in the same line, which would also return None
for the same reason as they operate inplace
by default.
通过在纯 python 中执行list.append()
, list.sort()
etc 操作,同时将它们的结果分配给同一行中的变量,可以观察到类似的类比,这也将返回None
与inplace
默认操作相同的原因。
Edit: Added a close parenthesis.
编辑:添加了一个右括号。