Issue
I'm cleaning up a dataset for plotting. Pretty standard stuff, transposing, deleting unnecessary columns, etc. Here is the code so far:
#File name
fname = r'E:\Grad School\Research\BOEM\Data\Grain Size\ForScript\allmyCoresRunTD.csv'
#Read csv file into a dataframe. skips the first row that just has the 'record number'. Also only loads the #Dx rows and not the remainder of the bins.
df = pd.read_csv(fname, index_col=None, skiprows=1, nrows=8)
#Delete 'Average' from Malvern. The script will average the data for us
df.drop(columns = df.columns[df.columns.str.startswith('Average')], inplace=True)
df.drop(columns = df.columns[df.columns.str.startswith('FSH')], inplace=True)
This is what the dataframe looks like at this point:
I rearrange the data some:
#Transpose the data
df2 = df.transpose()
#Change the first row into column names
df2.columns = df2.iloc[0]
#Delete header in first row
df2 = df2.drop('Sample Name', axis=0)
So then I just want to reset the index and name the new column SampleID.
df2 = df2.reset_index(names=['SampleID'])
But instead of moving the index to a column and naming the column SampleID, it gives the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 21
18 df2 = df2.drop('Sample Name', axis=0)
20 #Reset the indexes
---> 21 df2 = df2.reset_index(names='SampleID')
22 #df2['SampleID'] = df2['SampleID'].str.replace('19OCS-', '')
23 df2['SampleID'] = df2['SampleID'].str.split('.').str[0]
File ~\Anaconda3\lib\site-packages\pandas\util\_decorators.py:311, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs)
305 if len(args) > num_allow_args:
306 warnings.warn(
307 msg.format(arguments=arguments),
308 FutureWarning,
309 stacklevel=stacklevel,
310 )
--> 311 return func(*args, **kwargs)
TypeError: reset_index() got an unexpected keyword argument 'names'
I originally wrote this code about 6 months ago and it worked just fine but now it's not working? I only changed the file path in the code now vs 6 months ago.
I can run the line like this:
df2 = df2.reset_index()
And it will move the index correctly but, of course, the column is not named.
I can use .rename to change the name:
df2 = df2.rename({'index':'SampleID'}, axis=1)
But I can't figure out why the names keyword is not working. I've double checked my syntax and looked at the API and I just cannot figure out why it's giving me this error.
I've tried adding/removing brackets; I've tried specifying the level; I've tried using col_fill. It's probably something simple I did wrong. Thank you for your time.
Note: It changes from df to df2 after the transpose but I continue working in df2 for the remainder of the code.
Solution
Check that version of pandas you are running is >= 1.5.0. The parameter was introduced in 1.5.0 for reset_index()
import pandas as pd
pd.__version__
Answered By - 0x00
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.