[英]Python Pandas Replace Values with NAN from Tuple
Got the Following Dataframe:得到以下数据框:
A B
Temp1 1
Temp2 2
NaN NaN
NaN 4
Since the A nad B are correlated, I am able to create new column where I have calculated the nan value of A and B and form a tuple:由于 A 和 B 是相关的,我可以创建新列,在其中计算 A 和 B 的 nan 值并形成一个元组:
A B C
Temp1 1 (1,Temp1)
Temp2 2 (2, Temp2)
NaN NaN (3, Temp3)
NaN 4 (4, Temp4)
Now I have to drop the column C and fill the Nan value corrosponding to the Columns.现在我必须删除 C 列并填充与列对应的 Nan 值。
Use Series.fillna
with select values in tuple by indexing with str
, last remove C
column:通过使用
str
建立索引,将Series.fillna
与元组中的选择值一起使用,最后删除C
列:
#if values are not in tuples
#df.C = df.C.str.strip('()').str.split(',').apply(tuple)
df.A = df.A.fillna(df.C.str[1])
df.B = df.B.fillna(df.C.str[0])
df = df.drop('C', axis=1)
print (df)
A B
0 Temp1 1
1 Temp2 2
2 Temp3 3
3 Temp4 4
Or create DataFrame from C
with DataFrame.pop
for use and remove column, set new columns names and pass to DataFrame.fillna
:或创建数据框中
C
与DataFrame.pop
使用和删除列,设置新的列名,并传递给DataFrame.fillna
:
#if values are not in tuples
#df.C = df.C.str.strip('()').str.split(',').apply(tuple)
df[['A','B']] = df[['A','B']].fillna(pd.DataFrame(df.pop('C').tolist(), columns=['B','A']))
print (df)
A B
0 Temp1 1
1 Temp2 2
2 Temp3 3
3 Temp4 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.