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.
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
?
pandas. DataFrame. dropna() is used to drop/remove columns with NaN / None values.
You can use DataFrame. fillna or Series. fillna which will replace the Python object None , not the string 'None' .
NoneType is a type of a None Object in Python. The None keyword is an object, a data type of the class NoneType. We can assign None to any variable, but we can not create other NoneType objects. NoneType is simply the type of the None singleton. print(type(None))
How to Fix the KeyError? We can simply fix the error by correcting the spelling of the key. If we are not sure about the spelling we can simply print the list of all column names and crosscheck.
You would need to rewrite the line :
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.
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.
Edit: Added a close parenthesis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With