python更改数据框指定位置的数据_python-在Pandas中创建空数据框以指定列类型

遇到相同问题后,我发现了这个问题。 我更喜欢以下解决方案(Python 3),用于创建没有索引的空DataFrame。

import numpy as np

import pandas as pd

def make_empty_typed_df(dtype):

tdict = np.typeDict

types = tuple(tdict.get(t, t) for (_, t, *__) in dtype)

if any(t == np.void for t in types):

raise NotImplementedError('Not Implemented for columns of type "void"')

return pd.DataFrame.from_records(np.array([tuple(t() for t in types)], dtype=dtype)).iloc[:0, :]

测试一下...

from itertools import chain

dtype = [('col%d' % i, t) for i, t in enumerate(chain(np.typeDict, set(np.typeDict.values())))]

dtype = [(c, t) for (c, t) in dtype if (np.typeDict.get(t, t) != np.void) and not isinstance(t, int)]

print(make_empty_typed_df(dtype))

出:

Empty DataFrame

Columns: [col0, col6, col16, col23, col24, col25, col26, col27, col29, col30, col31, col32, col33, col34, col35, col36, col37, col38, col39, col40, col41, col42, col43, col44, col45, col46, col47, col48, col49, col50, col51, col52, col53, col54, col55, col56, col57, col58, col60, col61, col62, col63, col64, col65, col66, col67, col68, col69, col70, col71, col72, col73, col74, col75, col76, col77, col78, col79, col80, col81, col82, col83, col84, col85, col86, col87, col88, col89, col90, col91, col92, col93, col95, col96, col97, col98, col99, col100, col101, col102, col103, col104, col105, col106, col107, col108, col109, col110, col111, col112, col113, col114, col115, col117, col119, col120, col121, col122, col123, col124, ...]

Index: []

[0 rows x 146 columns]

以及数据类型...

print(make_empty_typed_df(dtype).dtypes)

出:

col0 timedelta64[ns]

col6 uint16

col16 uint64

col23 int8

col24 timedelta64[ns]

col25 bool

col26 complex64

col27 int64

col29 float64

col30 int8

col31 float16

col32 uint64

col33 uint8

col34 object

col35 complex128

col36 int64

col37 int16

col38 int32

col39 int32

col40 float16

col41 object

col42 uint64

col43 object

col44 int16

col45 object

col46 int64

col47 int16

col48 uint32

col49 object

col50 uint64

...

col144 int32

col145 bool

col146 float64

col147 datetime64[ns]

col148 object

col149 object

col150 complex128

col151 timedelta64[ns]

col152 int32

col153 uint8

col154 float64

col156 int64

col157 uint32

col158 object

col159 int8

col160 int32

col161 uint64

col162 int16

col163 uint32

col164 object

col165 datetime64[ns]

col166 float32

col167 bool

col168 float64

col169 complex128

col170 float16

col171 object

col172 uint16

col173 complex64

col174 complex128

dtype: object

添加索引会很棘手,因为大多数数据类型都没有真正的缺失值,因此最终将它们转换为具有自然缺失值的其他类型(例如,将ints转换为2976930505930834834945或objects),但是如果您已经完成了 指定类型的数据,那么您始终可以根据需要插入行,并且将尊重您的类型。 这可以通过以下方式完成:

df.loc[index, :] = new_row

再次,正如@Hun指出的,这不是熊猫如何被使用的方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值