K
K
Kirill Petrov2020-05-25 14:44:31
Python
Kirill Petrov, 2020-05-25 14:44:31

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

and
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

And I want, without changing the indexes of the first table, to assign the nearest previous value by index from the second table.

Here is such a poop code turned out to implement whether it would work:
# df1 первая таблица, df2 вторая
for index, item in df1.iterrows():
  df1.at[index, 'paramJoined'] = df2[df2.index <= index].param.tail(1).values[0]

And got the correct result:
index	param	paramJoined
0	-2	-3
1	-2	1
2	0	1
3	2	3


But this method is not productive with a large amount of data, tell me how to properly format this algorithm?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Petrov, 2020-05-25
@Recosh

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

But if someone makes it more beautiful, I will be grateful)

Z
zexer, 2020-05-25
@zexer

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 question

Ask a Question

731 491 924 answers to any question