请看下面的Python Pandas DataFrame:
ID | country | money | code | money_add | other | time |
---|---|---|---|---|---|---|
832932 | Other | NaN | 00000 | NaN | [N2,N2,N4] | 0 days 01:37:00 |
217#8# | NaN | NaN | NaN | NaN | [N1,N2,N3] | 2 days 01:01:00 |
1329T2 | France | 12131 | 00020 | 3452 | [N1,N1] | 1 days 03:55:00 |
124932 | France | NaN | 00016 | NaN | [N2] | 0 days 01:28:00 |
194022 | France | NaN | 00000 | NaN | [N4,N3] | 3 days 02:35:00 |
如果code
列不是NaN
,而money
列是NaN
,我们将更新下表中的值money
和money_add
.使用code
和cod_t
列作为键.
cod_t | money | money_add |
---|---|---|
00000 | 4532 | 72323 |
00016 | 1213 | 23822 |
00030 | 1313 | 8393 |
00020 | 1813 | 27328 |
生成的表示例:
ID | country | money | code | money_add | other | time |
---|---|---|---|---|---|---|
832932 | Other | 4532 | 00000 | 72323 | [N2,N2,N4] | 0 days 01:37:00 |
217#8# | NaN | NaN | NaN | NaN | [N1,N2,N3] | 2 days 01:01:00 |
1329T2 | France | 12131 | 00020 | 3452 | [N1,N1] | 1 days 03:55:00 |
124932 | France | 1213 | 00016 | 23822 | [N2] | 0 days 01:28:00 |
194022 | France | 4532 | 00000 | 72323 | [N4,N3] | 3 days 02:35:00 |
User@jezrael为我提供了以下问题解决方案:
df1 = df1.drop_duplicates('cod_t').set_index('cod_t')
df = df.set_index(df['code'])
df.update(df1, overwrite=False)
df = df.reset_index(drop=True).reindex(df.columns, axis=1)
但是这段代码给了我一个我不知道如何解决的错误:
TypeError: The DType <class 'numpy.dtype[timedelta64]'> could not be promoted by <class
'numpy.dtype[float64]'>. This means that no common DType exists for the given inputs.
For example they cannot be stored in a single array unless the dtype is `object`.
The full list of DTypes is: (<class 'numpy.dtype[timedelta64]'>, <class 'numpy.dtype[float64]'>)
// First DataFrame dtypes
ID object
country object
code object
money float64
money_add float64
other object
time timedelta64[ns]
dtype: object
// Second DataFrame dtypes
cod_t object
money int64
money_add int64
dtype: object
如果你能帮我解决这个错误,或者建议使用update
以外的其他方法,我将不胜感激.