How to get missing date in columns using python pandas [closed]

2024/9/20 7:10:57

I have following data frame in which i want to get missing date along with its keys in pandas.

     size     number     key      date
0  153.2 K    12345     Hello     20181001
1  153.2 K    12345     No        20181001
2  153.2 K    12345     Hello     20181003
3  153.2 K    12345     No        20181003
4  153.2 K    12345     Hello     20181004
5  153.2 K    12345     No        20181004

I want the output as,

  key      dateHello     20181002No        20181002

thanks in advance

Answer

You can do this with some fancy reshaping like this:

(df.pivot('date', 'key').reindex(np.arange(df['date'].min(), df['date'].max()+1)).stack('key', dropna=False).loc[lambda x: x['size'].isna()].index.to_frame(index=False))

Output:

       date    key
0  20181002  Hello
1  20181002     No

How?

  • Reshape the dataframe such that you have a single date per row

  • Next, reindex the dataframe to fill in missing dates

  • Reshape the dataframe stacking key but keeping NaN values

  • Filter dataframe to only missing values using isna

  • Convert the index to a dataframe with to_frame

Update address date concern mentioned by @Cimbali below

df['date'] = pd.to_datetime(df['date'], format='%Y%m%d')
(df.pivot('date', 'key').reindex(pd.date_range(df['date'].min(), df['date'].max(), freq='D')).stack('key', dropna=False).loc[lambda x: x['size'].isna()].index.to_frame(index=False))

Output:

           0    key
0 2018-10-02  Hello
1 2018-10-02     No
https://en.xdnf.cn/q/119357.html

Related Q&A

vigenere cipher - not adding correct values

I want to get specific values from a for loop to add to another string to create a vigenere cipher.heres the code.userinput = input(enter message) keyword = input(enter keyword) new = for a in keyword…

Why isnt my output returning as expected?

So I wrote this code def diagsDownRight(M):n = len(M)m = [[] * (n - i - 1) + row + [] * i for i, row in enumerate(M)]return ([.join(col) for col in zip(*m)]), [.join(col[::-1]) for col in zip(*m)] def …

Django Stripe payment does not respond after clicking the Submit Payment button

I have an e-commerce application that Im working on. The app is currently hosted on Heroku free account. At the moment I can select a product, add it on the cart and can get up to the stripe form and t…

get file path using backslash (\) in windows in python [duplicate]

This question already has answers here:How can I put an actual backslash in a string literal (not use it for an escape sequence)?(4 answers)Closed 2 years ago.How to get result exactly the same format…

Printing progress bar on a console without the use of for -loop

I have a script written in python, where I have a statement:Process.open() //some parametersWhich executes a command and puts the output on the console ,where I do not know the time taken to execute t…

ModuleNotFoundError: No module named verovio

Hi there I would like to run my flask app in a container but I got stucked caused of a third party module. (I am using PyCharm)This is my docker file:FROM python:3-alpineMAINTAINER fooCOPY app /appWORK…

Python: TypeError: list object is not callable on global variable

I am currently in the process of programming a text-based adventure in Python as a learning exercise. I want "help" to be a global command, stored as values in a list, that can be called at (…

Python beautifulsoup how to get the line after href

I have this piece of html:<a href="http://francetv.fr/videos/alcaline_l_instant_,12163184.html" class="ss-titre">"Paris Combo" </a> <…

Scrapy empty output

I am trying to use Scrapy to extract data from page. But I get an empty output. What is the problem? spider: class Ratemds(scrapy.Spider):name = ratemdsallowed_domains = [ratemds.com]custom_settings =…

nested classes - how to use function from parent class?

If I have this situation:class Foo(object):def __init__(self):self.bar = Bar()def do_something(self):print doing somethingclass Bar(object):def __init(self):self.a = adef some_function(self):I want to …