Combine two pandas DataFrame into one new

2024/10/14 3:17:34

I have two Pandas DataFrames whose data from different sources, but both DataFrames have the same column names. When combined only one column will keep the name.

Like this:

speed_df = pd.DataFrame.from_dict({'ts':  [0,1,3,4],'val': [5,4,2,1]})temp_df = pd.DataFrame.from_dict({'ts':  [0,1,2],'val': [9,8,7]})

And I need to have a result like this:

final_df = pd.DataFrame.from_dict({'ts':    [0,1,2,3,4],'speed': [5,4,NaN,1],'temp':  [9,8,7,NaN,NaN]})

Later I will deal with empty cells (here filled with NaN) by copying the values of the previous valid value. And get something like this:

final_df = pd.DataFrame.from_dict({'ts':    [0,1,2,3,4],'speed': [5,4,4,1],'temp':  [9,8,7,7,7]})
Answer

Use pd.merge

In [406]: (pd.merge(speed_df, temp_df, how='outer', on='ts').rename(columns={'val_x': 'speed','val_y': 'temp'}).sort_values(by='ts'))
Out[406]:ts  speed  temp
0   0    5.0   9.0
1   1    4.0   8.0
4   2    NaN   7.0
2   3    2.0   NaN
3   4    1.0   NaNIn [407]: (pd.merge(speed_df, temp_df, how='outer', on='ts').rename(columns={'val_x': 'speed', 'val_y': 'temp'}).sort_values(by='ts').ffill())
Out[407]:ts  speed  temp
0   0    5.0   9.0
1   1    4.0   8.0
4   2    4.0   7.0
2   3    2.0   7.0
3   4    1.0   7.0
https://en.xdnf.cn/q/118003.html

Related Q&A

Reasons of slowness in numpy.dot() function and how to mitigate them if custom classes are used?

I am profiling a numpy dot product call. numpy.dot(pseudo,pseudo)pseudo is a numpy array of custom objects. Defined as:pseudo = numpy.array([[PseudoBinary(1), PseudoBinary(0), PseudoBinary(1)],[PseudoB…

How to open cmd and run ipconfig in python

I would like to write a script that do something like that: open the cmd and run the commend "ipconfig" and than copy my ip and paste it to a text file. I wrote the beginning of the script …

Using OAuth to authenticate Office 365/Graph users with Django

We are creating an application for use in our organization, but we only want people in our organization to be able to use the app. We had the idea of using Microsofts OAuth endpoint in order to authent…

Python flatten array inside numpy array

I have a pretty stupid question, but for some reason, I just cant figure out what to do. I have a multi-dimensional numpy array, that should have the following shape:(345138, 30, 300)However, it actual…

Peewee and Flask : Database object has no attribute commit_select

Im trying to use Peewee with Flask, but I dont understand why my database connection does not work.config.pyclass Configuration(object): DATABASE = {name: test,engine: peewee.MySQLDatabase,user: root,p…

for loop to create a matrix in python

I am trying to study the probability of having a zero value in my data and I have developed a code that outputs the value of a column of data when the other is zero which is what I need. But having to …

How to convert List of JSON frames to JSON frame

I want to convert List of JSON object ot Single JSON frameHere is my codefor i in user1:name=i.namepassword=i.passwordid1=i.iduser = { "name" : name,"password" : password,"id&q…

Python 2.7 The packaging package is required; normally this is bundled with this package

I expect this has to do with the cryptography module, but Im not sure.Traceback (most recent call last):File "<string>", line 11, in <module>File "c:\python27\lib\site-packag…

Couple the data in all possible combinations

I have data in column in two columns like thisId Value 1 a 2 f 1 c 1 h 2 aand Id like couple the data of the Value column in all possible combinations based on the same Id such as(a,c) (a,h)…

Python - Find date from string

Would anyone know a regex string or another method of obtaining the date and time from this string into variables? The position of the string could change, so line and char no would not work. This is …