There are the following data frames.
df = pd.DataFrame (np.array ([ [np.nan, np.nan, 0.1, 0.1], [np.nan, 0.0, 0.2, 0.4], [np.nan, np.nan, np.nan, 0.0], [0.8, 0.6, 0.4, 0.2], [np.nan, 1, 0.9, 1], ]))
I want to convert this to:
I tried to convert each industry into a list and return it to the data frame, but I abandoned because I didn't know how to eliminate missing values in the list.
[nan, nan, 0.1,0.1]
[nan, nan, nan, 0.0]
→ Cannot delete missing values
Answer # 1
It's basically the same as nomuken, but if you want to use the original Column name as it is, a little more processing is required.
import pandas as pd import numpy as np df = pd.DataFrame ( [[np.nan, np.nan, 0.1, 0.1], [np.nan, 0.0, 0.2, 0.4], [np.nan, np.nan, np.nan, 0.0], [0.8, 0.6, 0.4, 0.2], [np.nan, 1, 0.9, 1]], columns = ['col_1', 'col_2', 'col_3', 'col_4']) df = df.apply (lambda d: d.dropna (). reset_index (drop = True), axis = 1) .rename (pd.Series (df.columns.values), axis = 1) print (df) # col_1 col_2 col_3 col_4 # 0 0.1 0.1 NaN NaN # 1 0.0 0.2 0.4 NaN # 2 0.0 NaN NaN NaN # 3 0.8 0.6 0.4 0.2 # 4 1.0 0.9 1.0 NaN
col = df.columns.values df = df.apply (lambda d: d.dropna (). reset_index (drop = True), axis = 1) df.columns = col
You may also write as follows.
However, if this method deletes NaN and the total number of columns changes, an error will occur.
Answer # 2
import pandas as pd import numpy as np df = pd.DataFrame (np.array ([ [np.nan, np.nan, 0.1, 0.1], [np.nan, 0.0, 0.2, 0.4], [np.nan, np.nan, np.nan, 0.0], [0.8, 0.6, 0.4, 0.2], [np.nan, 1, 0.9, 1], ])) df = df.apply (lambda x: x.dropna (). reset_index (drop = True), axis = 1) print (df)
- python - how to handle pandas missing values
- i want to start the next process when the first process starts in python
- python - the values of other arrays change with the array
- python - unable to read and display table values in django
- python - timesleep () does not start properly
- python 27 - [python] i want to create a new dict by extracting common ones from dict type values
- python - i want to make a program that retrieves values one by one from a list into a function
- python 3x - i want to output the keys and values in a tuple without (),''
- python - array i want to erase all the values in an array
- python 3x - i want to start the b file screen by pressing the a file button
- python 3x - a i want to get the value of values of the line selected in the list of files
- python - how to store tkinter input values in mysql
- python 3x - syntax error: invalid or missing encoding declaration error solution when described in another file of python
- python 3x - exchange of values between classes
- python - typeerror occurs when removing a url with a regular expression
- python - how to use subscripts to represent the values of another list in a list
- python - if you convert the index to a date type, it will start in 1900
- python - pandas replacing values after a few seconds
- python - when will you start the startapp in django?
- [python] [tkinter] i want to get multiple checkbox values
- python - i want to generate a matrix from a csv file
- i want to automate excel copy and paste with python
- python - i want to create a new dataframe by repeating the work of extracting the average value of the dataframe
- python - how to keep getting csv data in a loop
- python - [scipy-interpolate] i want to interpolate the time information and coordinate data into data every second
- python - judgment if the value is nan or not a valid url
- python - pandasdataframe i want to match the time columns of two csv and load them
- python - randomly extract data from multiple data frames (no between data frames is the same)
- i want to extract only those that have common items in the dataset [python]
- python - i want to get the very first value of the corresponding condition in the time condition of pandas