Answer the question
In order to leave comments, you need to log in
How to properly merge table if index is not clear in pandas?
Greetings! Now I have this question. Let's say we have 2 tables:
index param paramJoined
0 -2 None
1 -2 None
2 0 None
3 2 None
index param
0.000000 -3
0.434783 -3
0.869565 1
1.304348 0
1.739130 1
2.173913 3
2.608696 3
3.043478 6
3.478261 4
3.913043 7
# df1 первая таблица, df2 вторая
for index, item in df1.iterrows():
df1.at[index, 'paramJoined'] = df2[df2.index <= index].param.tail(1).values[0]
index param paramJoined
0 -2 -3
1 -2 1
2 0 1
3 2 3
Answer the question
In order to leave comments, you need to log in
So far I've decided this:
#Переименовываем стобец
df2Renamed = df2.rename(columns={'param': 'paramJoined'})
#Объединяем и сортируем по индексу
df1concat = pd.concat([df1,df2Renamed]).sort_index()
#Заполняем пустышки
df1concat.paramJoined = df1concat.paramJoined.ffill()
#Удаляем вспомогательные данные
df1result = df1concat.dropna(subset=['param'])
df1result
Obviously, such an index in the table is some kind of mistake, this should not be, if you think reasonably.
An index is a row identifier, it most often does not contain information, and even more so, it should not carry real numbers (float).
But if you need to somehow merge these tables, then it seems to me that at first it is better to work separately on the index so that it is brought to the desired form, and then link it.
PS Share, what is your task, what do you have to fence this?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question