csvファイルでNaN値を取り除く方法は?パイソン

GUNTER SAMA:

全てよりもまず、私はこの問題についての答えがあると知っているが、それらのどれも今まで私のために働いていません。とにかく、私はすでにそのソリューションを使用しているが、あなたの答えを知っていただきたいと思います。

私はと呼ばれるcsvファイルを持っていますmbti_datasets.csv最初の列のラベルがありtype、2番目の列が呼び出されますdescription各行は、(そのそれぞれのタイプおよび説明を)新しい性格タイプを表します。

TYPE        | DESCRIPTION
 a          | This personality likes to eat apples...\nThey look like monkeys...\nIn fact, are strong people...
 b          | b.description
 c          | c.description
 d          | d.description
...16 types | ...

次のコードでは、私は記述がある場合、それぞれの性格タイプを複製しようとしています\n

コード:

import pandas as pd

# Reading the file
path_root = 'gdrive/My Drive/Colab Notebooks/MBTI/mbti_datasets.csv'
root_fn = path_rooth + 'mbti_datasets.csv'
df = pd.read_csv(path_root, sep = ',', quotechar = '"', usecols = [0, 1])

# split the column where there are new lines and turn it into a series
serie = df['description'].str.split('\n').apply(pd.Series, 1).stack()

# remove the second index for the DataFrame and the series to share indexes
serie.index = serie.index.droplevel(1)

# give it a name to join it to the DataFrame
serie.name = 'description'

# remove original column
del df['description']

# join the series with the DataFrame, based on the shared index
df = df.join(serie)

# New file name and writing the new csv file
root_new_fn = path_root + 'mbti_new.csv'

df.to_csv(root_new_fn, sep = ',', quotechar = '"', encoding = 'utf-8', index = False)
new_df = pd.read_csv(root_new_fn)

print(new_df)

予想される出力:

TYPE | DESCRIPTION
 a   | This personality likes to eat apples... 
 a   | They look like monkeys...
 a   | In fact, are strong people...
 b   | b.description
 b   | b.description
 c   | c.description
...  | ...

CURRENT OUTPUT:

TYPE | DESCRIPTION
 a   | This personality likes to eat apples...
 a   | They look like monkeys...NaN
 a   | NaN
 a   | In fact, are strong people...NaN
 b   | b.description...NaN
 b   | NaN
 b   | b.description
 c   | c.description
...  | ...

私は確かに100%ではないんだけど、私はNaN値があると思います\r

要求に応じてファイルはgithubのにアップロード: CSV FILES

@YOLOソリューションを使用する: CSV YOLOファイル 失敗している例えば:

2 INTJ  Existe soledad en la cima y-- siendo # adds -- in blank random blank spaces
3 INTJ  -- y las mujeres # adds -- in the beginning
3 INTJ  (...) el 0--8-- de la poblaci # doesnt end the word 'población'
10 INTJ icos-- un conflicto que parecer--a imposible. # starts letters randomly
12 INTJ c #adds just 1 letter

完全に理解するための翻訳:

2 INTJ There is loneliness at the top and-- being # adds -- in blank spaces
3 INTJ -- and women # adds - in the beginning
3 INTJ (...) on 0--8-- of the popula-- # doesnt end the word 'population'
10 INTJ icos-- a conflict that seems--to impossible. # starts letters randomly
12 INTJ c #adds just 1 letter

とき私は、任意のNaN値があるかどう表示し、どのタイプ:

print(new_df['descripcion'].isnull())

<class 'float'>
0     False
1     False
2     False
3     False
4     False
5     False
6     False
7      True
8     False
9      True
10    False
11     True
continue...
GUNTER SAMA:

二つの新しい連続した行を持つ部分があるような問題は、それらの間に何もないと、説明細胞に起因することができます。

私はちょうど使用し.dropna()作成した新しいCSVを読み取るために、そしてNaN値なしでそれを書き換えます。とにかく、私はこのプロセスを繰り返すことではない最良の方法だと思いますが、それは解決策としてストレートだろう。

df.to_csv(root_new_fn, sep = ',', quotechar = '"', encoding = 'utf-8', index = False)
new_df = pd.read_csv(root_new_fn).dropna()

new_df.to_csv(root_new_fn, sep = ',', quotechar = '"', encoding = 'utf-8', index = False)
new_df = pd.read_csv(root_new_fn)

print(type(new_df.iloc[7, 1]))# where was a NaN value
print(new_df['descripcion'].isnull())

<class 'str'>
0     False
1     False
2     False
3     False
4     False
5     False
6     False
7     False
8     False
and continues...

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=16162&siteId=1