Pandas Data Frame Select column last value and replace with blank - pandas

I have a data frame with lots of columns, I wanted to replace the last value of few of columns with blank, what is the best way to do that.
the thing is data frame can be dynamic with values and length.
Here is the data frame:
enter image description here
How can I remove values from column F and G to blank?
please except typos as I'm new here.

df.loc[df.index[-1], ['G', 'F']] = np.nan

Related

How can I query a column of a dataframe on a specific value and get the values of two other columns corresponding to that value

I have a data frame where the first column contains various countries' ISO codes, while the other 2 columns contain dataset numbers and Linkedin profile links.
Please refer to the image.
I need to query the data frame's first "FBC" column on the "IND" value and get the corresponding values of the "no" and "Linkedin" columns.
Can somebody please suggest a solution?
Using query():
If you want just the no and Linkedin values.
df = df.query("FBC.eq('IND')")[["no", "Linkedin"]]
If you want all 3:
df = df.query("FBC.eq('IND')")

Keyerror when looping over a data frame column

I had a dataset, and I want to create a new data frame from a column in the original one. Chessdata is the original data frame and hizlisatranc is the one that I'm trying to create.
However, it raises a keyerror. I couldn't fix it. Can someone please help?
for i in range(len(chessdata)):
a = chessdata.newtime[i]
if float(6)<=a<float(25):
hızlısatranç.append(chessdata.iloc[i])
else:
continue
you can filter the df, with values in a range of value1 to value 2, so that only the rows that meet that condition are left. You can assign that to a new df like so:
df_new = df_old[(df_old['column']>value1) & (df_old['column']<value2)]

How to remove points after characters in a specific column pandas

I loaded a large dataframe and I have a column with Ensembl values ex: ENGS00000000.1,ENGS003490.12 and etc. I am trying to remove the point and the values after the points. The DF itself has numeric values and contain float characters which mustn't removed.
df = pd.read_csv("Fileinfo1.csv")
df["Ensembl_ID"] = pd.set_option('precision', 1)
df.head()
If I did correctly, this is the code (not sure why but it doesn't print..) is there any option to remove them all from the column instead of changing the set_option everytime?

delete rows from pandas data frame that contains one of its columns as list , when one of its values match value in another compared list

delete rows from pandas data frame that contains one of its columns as list , when one of its values match value in another compared list column in another data frame.
here is the first data frame column: enter image description here
and the other data frame column is here: enter image description here
I have tried a lot of codes
Revdf=Revdf.drop(lambda x: [i for i in Revdf.AffiliationHistory if i in Authdf.Affiliations.values], axis=1)
or
Revdf=Revdf[~(Revdf.AffiliationHistory.isin(Authdf.Affiliations.values))]
but these can't help
There has to be an easier way, but i wrote a function for it and it works:
def remove_row(df1,x1,y1,df2,x2,y2):
assert type(df1.loc[x1,y1])==list,"type have to be list"
assert type(df2.loc[x2,y2])==list,"type have to be list"
flag =False
l1=df1.loc[x1,y1]
print(l1)
l2=df2.loc[x2,y2]
print(l2)
for i in l1:
if i in l2:
flag=True
break
if flag==True:
return df1.drop(x1)
else:
return df1
x is the row index, y is the column name, tried it on synthetic data and it works:
df1=pd.DataFrame({'col1':[0,0,0,0,1],
'col2':[[1,2,3,4],0,0,0,0]})
df2=pd.DataFrame({'col1':[0,0,0,0],
'col2':[[0,0,0,4],0,0,0]})
remove_row(df1,0,'col2',df2,0,'col2')
Also, i think a mistake you're making is this:
[1,2,3,4] in [0,1,2,3,4]
will return false, because you're asking if the second list contains the first.

How do I preset the dimensions of my dataframe in pandas?

I am trying to preset the dimensions of my data frame in pandas so that I can have 500 rows by 300 columns. I want to set it before I enter data into the dataframe.
I am working on a project where I need to take a column of data, copy it, shift it one to the right and shift it down by one row.
I am having trouble with the last row being cut off when I shift it down by one row (eg: I started with 23 rows and it remains at 23 rows despite the fact that I shifted down by one and should have 24 rows).
Here is what I have done so far:
bolusCI = pd.DataFrame()
##set index to very high number to accommodate shifting row down by 1
bolusCI = bolus_raw[["Activity (mCi)"]].copy()
activity_copy = bolusCI.shift(1)
activity_copy
pd.concat([bolusCI, activity_copy], axis =1)
Thanks!
There might be a more efficient way to achieve what you are looking to do, but to directly answer your question you could do something like this to init the DataFrame with certain dimensions
pd.DataFrame(columns=range(300),index=range(500))
You just need to define the index and columns in the constructor. The simplest way is to use pandas.RangeIndex. It mimics np.arange and range in syntax. You can also pass a name parameter to name it.
pd.DataFrame
pd.Index
df = pd.DataFrame(
index=pd.RangeIndex(500),
columns=pd.RangeIndex(300)
)
print(df.shape)
(500, 300)