0
$\begingroup$

I have a dataframe like this enter image description here

my desire format is like this enter image description here

how can i do this?

$\endgroup$
1
  • 1
    $\begingroup$ Please keep in mind that it is not a good practice to upload photos of your dataset and ask for help. Instead give an simple reproducible lines of codes even for your dataframe, like my answer below, that make it easier for the community to help you. Good luck! $\endgroup$ Oct 28 '18 at 16:25
2
$\begingroup$

Let's say you have the following data:

import pandas as pd
import numpy as np
df = pd.DataFrame({'values': ['1', '2', '3', '4', '5', '6'],
                  'month1': ['January', 'March', np.nan, np.nan, np.nan, np.nan],
                  'month2': [np.nan, np.nan, 'February', 'April', np.nan, np.nan],
                  'month3': [np.nan, np.nan, np.nan, np.nan, 'May', 'October']})

  values   month1    month2   month3
0      1  January       NaN      NaN
1      2    March       NaN      NaN
2      3      NaN  February      NaN
3      4      NaN     April      NaN
4      5      NaN       NaN      May
5      6      NaN       NaN  October

You can use the following solution:

df['month']=np.nan
for month in [col for col in df.columns if 'month' in col]:
    df['month'].fillna(df[month],inplace=True)

It first creates an empty column named "month" with NaN values, and you fill the NaN with the values from the "monthX" columns, concretely it gives you:

  values   month1    month2   month3     month
0      1  January       NaN      NaN   January
1      2    March       NaN      NaN     March
2      3      NaN  February      NaN  February
3      4      NaN     April      NaN     April
4      5      NaN       NaN      May       May
5      6      NaN       NaN  October   October
$\endgroup$
4
  • $\begingroup$ i tried it but i got this error TypeError: argument of type 'int' is not iterable how can i solve this problem? $\endgroup$ Oct 29 '18 at 8:29
  • $\begingroup$ Did you try my example and still get this error? Or you are using it on your own data? $\endgroup$ Oct 29 '18 at 9:41
  • $\begingroup$ i used it my dataframe but when i use it in another dataframe i got a error $\endgroup$ Oct 29 '18 at 11:13
  • $\begingroup$ If you use this method and get error on your data, then modify the question and provide a short version of data so that I can see what the problem is!! Otherwise no one can help you. $\endgroup$ Oct 29 '18 at 12:04
0
$\begingroup$

You should be able to compare to "nan" to get the required behavior:

df["month"] = df["month1"]

for month in ["month2", "month3"]:
    mask = df[month].values != "nan"
    df["month"][mask] = df[month][mask]
$\endgroup$
6
  • $\begingroup$ I got this error TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' how can i solve this problem? $\endgroup$ Oct 28 '18 at 15:06
  • $\begingroup$ Oops, sorry, missing .values for isnan. $\endgroup$ Oct 28 '18 at 15:11
  • $\begingroup$ still i got same error. is there any simple method ? $\endgroup$ Oct 28 '18 at 15:27
  • $\begingroup$ Oh indeed, depends on your data type. $\endgroup$ Oct 28 '18 at 15:44
  • $\begingroup$ my column datatype is object so how can i do then? $\endgroup$ Oct 28 '18 at 16:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.