- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
Pandas iterrows 改变列的类型。根据this github 问题,这是预期的行为。
有没有想过用 pythonic 和优雅的方式将其转换回原始类型?请注意,我有多种列类型。
最小的例子
df = pd.DataFrame([range(5), range(5)])
df.iloc[:,1] = df.iloc[:,1].astype('float')
for row in df.iterrows():
print row
结果
(0, 0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
Name: 0, dtype: float64)
(1, 0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
Name: 1, dtype: float64)
请注意,df.dtypes
返回列的类型,但是,我想不出一种优雅的方式来使用它将行转换回该类型。
最佳答案
尝试使用 df.itertuples
代替:
df = pd.DataFrame([range(5), range(5)], columns=list('abcde'))
df.iloc[:,1] = df.iloc[:,1].astype('float')
for row in df.itertuples():
print(row)
Pandas(Index=0, a=0, b=1.0, c=2, d=3, e=4)
Pandas(Index=1, a=0, b=1.0, c=2, d=3, e=4)
关于python - Pandas iterrows 将列的类型更改为 float 。如何将其转换回原始类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42438063/
我编写了以下代码来创建数据框,并根据特定条件添加新的行和列。不幸的是,执行起来需要很多时间。 有其他方法可以做到这一点吗?任何意见都将受到高度赞赏。 dfCircuito=None for index
我有一个pandas数据框 from pandas import DataFrame, Series 其中每一行对应一个案例,每一列对应一个月份。我想在每 12 个月期间执行一次滚动求和。看起来很简单
我知道这个话题已经被讨论了一千次了。但我想不出解决办法。 我正在尝试计算列表(df1.list1 的每一行)在列表 (df2.list2) 的列中出现的频率。所有列表仅包含唯一值。 List1 包括大
我有这段代码,其中有大约 60k 行。完成整个过程大约需要 4 个小时。这段代码不可行,时间紧迫,想用apply代替iterrow。 这是代码, all_merged_k = pd.DataFrame
我有这段代码,其中有大约 60k 行。完成整个过程大约需要 4 个小时。这段代码不可行,时间紧迫,想用apply代替iterrow。 这是代码, all_merged_k = pd.DataFrame
我需要从 6 列数据帧的单行中选择 3 列,并将其附加到另一个只有 3 列的数据帧。 import pandas import numpy df1 = pd.DataFrame({'Name':['S
这个问题已经有答案了: Create Pandas DataFrame from txt file with specific pattern (6 个回答) 已关闭 4 年前。 我正在研究代表美国地
以下是示例数据 - 当前解决方案 - for idx, row in data.iterrows(): rp = row['right_player_test'] lp = row['
当我为每个客户创建新的数据框架时,我还想创建一个将所有这些数据框架附加在一起的巨大数据框架。 我创建了一个函数来按照我需要的方式对用户数据进行分组。现在我想迭代另一个包含唯一用户 key 的数据帧,并
d = {'2014-07-10 08:26:29.091199': {'Col':'12976482066142964'}, '2014-07-10 08:26:29.171198': {'Col'
我有一个数据集,其中包含几年来每天的美国国债曲线。行 = 日期,列 = 特定国债期限(3 个月、1 年、10 年等) 我有每天循环并校准利率模型参数的Python代码。我在通过 iterrows 和循
我有一个 pandas Dataframe 的形式: A B K S 2012-03-31 NaN NaN
我讨论了 iterrows 的性能问题 previously , 并得到了良好的普遍 react 。这个问题是一个特定的案例,我希望你能帮助我更好地应用一些东西,因为 iterrows 很慢。 我相信
我在 pandas 中有一个 df,它看起来像: id name values 1 a cat dog 2 b bird fly 我目前在做: for index, row
我相信有人也在 this thread 中报告过, 使用 iterrows() 填充数据框可能导致持久性问题。例如。像这样简单的东西: my_dataframe = pd.DataFrame(np.N
以下代码读取 csv 文件并遍历“子”列的行: df = pd.read_csv("Subs_test.csv", usecols= ['sub']) for index, row in df.ite
我是编程新手,到目前为止,这个问题已经“超出了我的薪水等级”,因此非常感谢您对此提供的任何帮助! 我有一个如下所示的 DataFrame 对象: Dan
我有两个数据集。 df Name Date Quantity ZMTD 2018-06-30 1000 ZMTD 2018-05-31 975 ZMT
我想知道在使用 iterrows 遍历 DataFrame 时如何使用索引而不是名称访问列。 这段代码是我能找到的最多的: for index, row in df.iterrows(): p
我有两个非常相似的 iterrows 循环,一个按我的预期工作,另一个不工作。我的问题是什么使第二个与第一个不同,为什么这会改变我的循环的行为?谢谢 工作循环 我正在使用数据框 drugs_tall
我是一名优秀的程序员,十分优秀!