Pandas melt dataframe based on datetime and fill all values NaN


Tony Brand

I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20".

Here is my dataframe column as shown below.

Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20...

I try to fuse a dataframe to get values ​​like datetime and country

US 1/25/20 28

but all values ​​are defined asNaN

Australia   2020-01-22  NaN

How can I fix this?

Here is my code snippet

def meltDataFrame(df,id_vars,value_vars,var_name,value_name):
    return pd.melt(df,
        id_vars= id_vars,
        value_vars = value_vars,
        var_name= var_name,
        value_name= value_name)

data_df_melt = meltDataFrame(data_df.reset_index(),
        ['Country'],pd.date_range('1/22/20', '3/18/20', freq='D'),'Date','Total_Count')
Jesler

The problem is that the column names are not datetimes.

So without converting all column names to datetime first:

df.columns = df.columns[:1].tolist() + pd.to_datetime(df.columns[1:]).tolist()

Then melt.

Sample :

print (df)
     Country  1/22/20  1/23/20  1/24/20
0  Australia       11       42       53

df.columns = df.columns[:1].tolist() + pd.to_datetime(df.columns[1:]).tolist()
print (df)
     Country  2020-01-22 00:00:00  2020-01-23 00:00:00  2020-01-24 00:00:00
0  Australia                   11                   42                   53

Related


Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get

Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get

Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get

Pandas melt dataframe based on datetime and fill all values NaN

Tony Brand I have a dataframe with all countries and datetimes ranging from "1/22/20" to "2/22/20". Here is my dataframe column as shown below. Country 1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20... I try to fuse a dataframe to get

How to fill NaN values based on data type in pandas dataframe?

Manish Chaudhary Greetings everyone. I have an Excel file and I need to clear and fill NaN values based on the column data type, for example if the column data type is object, I need to fill the column with "NULL" and if the data type is integer or float 0, I

How to fill NaN values based on data type in pandas dataframe?

Manish Chaudhary Greetings everyone. I have an Excel file and I need to clear and fill NaN values based on the column data type, for example if the column data type is object, I need to fill the column with "NULL" and if the data type is integer or float 0, I

How to fill NaN values based on data type in pandas dataframe?

Manish Chaudhary Greetings everyone. I have an Excel file and I need to clear and fill NaN values based on the column data type, for example if the column data type is object, I need to fill the column with "NULL" and if the data type is integer or float 0, I

How to fill NaN values based on data type in pandas dataframe?

Manish Chaudhary Greetings everyone. I have an Excel file and I need to clear and fill NaN values based on the column data type, for example if the column data type is object, I need to fill the column with "NULL" and if the data type is integer or float 0, I

How to fill NaN values based on data type in pandas dataframe?

Manish Chaudhary Greetings everyone. I have an Excel file and I need to clear and fill NaN values based on the column data type, for example if the column data type is object, I need to fill the column with "NULL" and if the data type is integer or float 0, I

How to fill NaN values based on data type in pandas dataframe?

Manish Chaudhary Greetings everyone. I have an Excel file and I need to clear and fill NaN values based on the column data type, for example if the column data type is object, I need to fill the column with "NULL" and if the data type is integer or float 0, I

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c

Pandas - if all values of dataFrame are NaN

math student How to create an if statement that does the following: if all values in dataframe are nan: do something else: do something else According to this post , it is possible to check if all values of a DataFrame are NaN. I know that one c