I'm trying to merge dataframes but it raised errors and I don't know how to solve. Below is my sample code:
import pandas as pd
df1 = pd.read_csv('https://raw.githubusercontent.com/hoatranobita/Jobs/main/UMAPdf (3).csv')
counts_url = "https://www.dropbox.com/s/pivx6ktd4zfj4jn/counts.csv?dl=1"
df2 = pd.read_table(counts_url,sep=',', header=(0))
df3 = df2.T
df4 = pd.read_table("https://raw.githubusercontent.com/hoatranobita/Jobs/main/annotated_clusters.csv", sep=',', index_col=1)
df4 = df4.rename(columns={"cluster": "clusters"}, errors="raise")
df4.clusters = df4.clusters.astype('str'
selected_gene = df4.index
sel_len = len(selected_gene)
#sel_len = int(sel_len)
for i in range(sel_len):
df1[selected_gene[i]] = df3[selected_gene[i]].values
df1
With this code it raised errors as below:
if is_scalar(key) and isna(key) and not self.hasnans:
KeyError: 'SERPINB13'
I understand that have some mismatch data between dataframes but I don't know how to skip errors. I mean that how can I skip KeyError
from loop.
Below is my desire Ouput:
UMAP1 UMAP2 UMAP3 Id clusters AKR1C1 PERP BIRC5 MS4A6A CTSL TRBC2 JCHAIN SCEL PLK1 SCGB3A1 DCN LPL VWF RGS5
0 8.114442 4.716549 9.492684 X24_CTGACACAATGC 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0
1 9.849996 5.723419 9.249526 X24_CTGGTTAGAGTA 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
2 5.677463 1.173194 5.189664 X24_AGAGCATATACA 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 8.003273 4.559005 9.536844 X24_ATTTCTGGTGCT 0 0 2 0 0 0 0 0 3 0 0 0 0 0 0
4 9.555323 5.731429 9.267872 X24_AATCTGACAGCT 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0
Thank you.
CodePudding user response:
You could use something like this:
try:
"code"
except IndexError: # IndexError as an example
pass