Home>
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],
]))
col_1 | col_2 | col_3 | col_4 |
---|---|---|---|
NaN | NaN | 0.1 | 0.1 |
NaN | 0.0 | 0.2 | 0.4 |
NaN | NaN | NaN | 0.0 |
0.8 | 0.6 | 0.4 | 0.2 |
NaN | 1 | 0.9 | 1 |
I want to convert this to:
col_1 | col_2 | col_3 | col_4 |
---|---|---|---|
0.1 | 0.1 | NaN | NaN |
0.0 | 0.2 | 0.4 | NaN |
0.0 | NaN | NaN | NaN |
0.8 | 0.6 | 0.4 | 0.2 |
1 | 0.9 | 1 | NaN |
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.
col_1
[nan, nan, 0.1,0.1]
[nan, 0.0,0.2,0.4]
[nan, nan, nan, 0.0]
[0.8,0.6,0.4,0.2]
[nan, 1,0.9,1]
→ Cannot delete missing values
-
Answer # 1
-
Answer # 2
Kana ...
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)
Related articles
- python - jupyterlab does not start
- python - read and write values from another file
- python - how to use subscripts to represent the values of another list in a list
- 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 retrieve only even values in the list using an if statement
- i want to start a process that is completely independent of python
- python - how to list only specific values in a dictionary
- python - unable to start jupyter lab
- python 3x - valueerror: not enough values to unpack (expected 3, got 2)
- python 3x - i want to get a matrix with the values of a certain column removed for each row of the matrix with pytorch (speedu
- python - i want to remove whitespace in dictionary values
- python - selenium does not start properly
- python - is it possible to bring the values of the list in params from the fixture when parameterizing the fixture with pytest
- python - i want to display the maximum and minimum values from the n real numbers i entered
- python - if you convert the index to a date type, it will start in 1900
- i want to start the next process when the first process starts in python
- command does not start in python discordbot
- unable to get database values in python django
Related questions
- python : Need help finding the index on the Series object s
- python : How to remove data gaps in the set date
- python : I want to subtract each column of dataframe by the value of the first column
- python : Need to parse and convert CSV data to JSON
- Help with kNN visualization, in python
- python : Sorting by multiple columns in a dataframe
- python : Identifying parts of speech and removing unnecessary parts
- python : Get unique values in dataframe rows
- Extract specific columns with Python and email
- python : Add DataFrame to Excel file without overwriting it
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.
or simply
You may also write as follows.
However, if this method deletes NaN and the total number of columns changes, an error will occur.