Delete second row of pandas dataframe - python

I am trying to delete the second row of pandas dataframe but I was unable to do so.
Code used:
df1.drop([1,],axis=0, inplace=True)
Note: The second row is empty that's why I want it gone.

This one works for me
df1.drop(1, inplace=True)

Related

Filling In Empty Dates With Previous Data in Pandas

So I have the current file in Excel where I have dates and don't have dates for everything which can be seen.
I read this excel file into a pandas dataframe, rename the column and get the following:
My question is, how would I get it so every empty date in the dataframe is filled in with the last previous date encountered. All of the blanks between 04/03/2021 and 05/03/2021 gets replaced with 04/03/2021, so every row in my dataframe has a date associated with it?
Thanks!
After reading the data into a dataframe, you can fill missing values using fillna with method='ffill' for forward fill
Just using the inbuilt way in pandas of:
duplicate_df['StartDate'] = duplicate_df['StartDate'].fillna(method = 'ffill')
This replaces all the NaNs in the dataframe with the last row that had data in.

How should I delete the rows in Pandas dataframe?

I have a dataframe which has 14343 rows. But, when I check df.info() it shows 14365 rows as after the last row, there are cells which explain the column names and df considers it as a row. I tried the following code but it seems it did not work: df.drop(df.index[14344, ])

pandas conditional drop rows in for loop

My Pandas DataFrame has 17543 rows. I want to drop a row, only if every column contains 'nan'. I tried instructions as per the link drop rows in for loop
but did not help. The following is my code
NullRows=0
for i in range(len(SetMerge.index)):
if(SetMerge.iloc[i].isnull().all()):
df=SetMerge.drop(SetMerge.index[i])
NullRows +=1
print("total null rows : ", NullRows)
I get only one row dropped in df with 17542 rows whereas NullRows output is 30.
drop doesn't mutate your SetMerge. Thus, you need to re-assign SetMerge after drop, or use another function.
It is written in answer, by link which you've posted here and checked. Specify inplace=True option for mutation.

Delete first column and then take them as a index pandas

I have a word2vec dataframe like this which saved from save_word2vec_format using Gensim under txt file. After using pandas to read this file. (Picture below). How to delete first row and make them as a index?
My txt file: https://drive.google.com/file/d/1O206N93hPSmvMjwc0W5ATyqQMdMwhRlF/view?usp=sharing
try this,
to replace index as header,
_X_T.index=_X_T.columns
to replace first row as header,
_X_T.index=_X_T.iloc[0]
save the row:
new_index = df.iloc[0]
drop it to avoid length mismatch:
df.drop(df.index[0], inplace=True)
and set it:
df.set_index(new_index, inplace=True)
you will get a SettingWithCopyWarning but that's the most elegant solution i could come up with.
if you want to set the headers (and not the first row) do:
df.index = df.columns

Shrink down row numbers in a Pandas Dataframe when removing rows in Python

Essentially this is the same question as in this link:How to automatically shrink down row numbers in R data frame when removing rows in R. However, I want to do this with a pandas dataframe. How would I go about doing so? There seems to be nothing similar to the rownames method of R dataframes in the Pandas library...Any ideas?
What you call "row number" is part of the index in pandas-speak, in this case a integer index. You can rebuild the index using
df = df.reset_index(drop=True)
There is another way of doing this, which does not generate a new column with the old index:
df.index=range(len(df.index))

Categories