filmov
tv
Python - Add Missing Data to New Dataframe

Показать описание
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Python - Add Missing Data to New Dataframe
I am comparing two dataframes by looping through both and determining if data (row) from one does not exist in the other. If there is no match, I want to add that specific row to a third dataframe. I was able to achieve this with the append() method, but Python warns me that it is deprecated and tells me to use concat instead. Unfortunately, I cannot get concat to achieve the same result.
import pandas as pd
df1 = pd.DataFrame(
{
"Animal": ["Corgi",
"Labrador",
"Orange Cat",
"Black Cat"],
"Owner": ["Bob",
"Joe",
"Sara",
"Glen"]
})
df2 = pd.DataFrame(
{
"Animal": ["Corgi",
"Labrador",
"Black Cat"],
"Owner": ["Bob",
"Joe",
"Glen"]
})
dfF = pd.DataFrame(columns=["Animal", "Owner"])
if df1Sort["Animal"][ind] == df2Sort["Animal"][subInd]:
print("Found match:",
df1Sort["Animal"][ind],
"-",
df1Sort["Owner"][ind])
break
print("No match for:",
df1Sort["Animal"][ind],
"-",
df1Sort["Owner"][ind])
break
print(dfF)
import pandas as pd
df1 = pd.DataFrame(
{
"Animal": ["Corgi",
"Labrador",
"Orange Cat",
"Black Cat"],
"Owner": ["Bob",
"Joe",
"Sara",
"Glen"]
})
df2 = pd.DataFrame(
{
"Animal": ["Corgi",
"Labrador",
"Black Cat"],
"Owner": ["Bob",
"Joe",
"Glen"]
})
dfF = pd.DataFrame(columns=["Animal", "Owner"])
if df1Sort["Animal"][ind] == df2Sort["Animal"][subInd]:
print("Found match:",
df1Sort["Animal"][ind],
"-",
df1Sort["Owner"][ind])
break
print("No match for:",
df1Sort["Animal"][ind],
"-",
df1Sort["Owner"][ind])
break
print(dfF)
The output is:
Found match: Black Cat - Glen
Found match: Corgi - Bob
Found match: Labrador - Joe
No match for: Orange Cat - Sara
Animal Owner 0
0 NaN NaN Orange Cat
1 NaN NaN Sara
Found match: Black Cat - Glen
Found match: Corgi - Bob
Found match: Labrador - Joe
No match for: Orange Cat - Sara
Animal Owner 0
0 NaN NaN Orange Source of the question:
Question and source license information: