- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试将 2 个数据帧合并在一起。具有讽刺意味的是,它们最初是同一个数据框的一部分,但我正在迈出一小步——有时是在错误的方向上。第 1 帧看起来像这样:
Int64Index: 10730 entries, 0 to 10729Data columns (total 6 columns):RegionID 10730 non-null int64RegionName 10730 non-null objectState 10730 non-null objectMetro 10259 non-null objectCountyName 10730 non-null objectSizeRank 10730 non-null int64dtypes: int64(2), object(4)
Frame 2 looks like this:
Int64Index: 10730 entries, 0 to 10729Data columns (total 82 columns):1996Q2 8218 non-null float641996Q3 8229 non-null float641996Q4 8235 non-null float64.....2016Q1 10730 non-null float642016Q2 10730 non-null float642016Q3 10730 non-null float64dtypes: float64(82)
Notice that the indexes are of the same type, and they even have the same number of rows.
I am trying to merge the dataframes back together like so:
df4 = pd.merge(df3, df2, how='inner', left_index=True, right_index=True)
我得到的错误是:
ValueError: can only call with other PeriodIndex-ed objects
第二个数据框中的 2016Q1 和类似名称的列是 Period 类型,但我没有合并它们——我认为只要索引排成一行,合并就应该起作用?我做错了什么?
最佳答案
假设我们有以下 DF:
In [44]: df1
Out[44]:
1996Q2 2000Q3 2010Q4
0 1.5 3.5 1.000000
1 22.0 38.5 2.000000
2 15.0 35.0 4.333333
In [45]: df1.columns
Out[45]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')
注意:df1.columns
属于PeriodIndex
数据类型
In [46]: df2
Out[46]:
a b c
0 a1 b1 c1
1 a2 b2 c2
2 a3 b3 c3
In [47]: df2.columns
Out[47]: Index(['a', 'b', 'c'], dtype='object')
merge
和 join
将返回:ValueError: can only call with other PeriodIndex-ed objects
因为,据我所知,Pandas DF 不能如果其中一些属于 PeriodIndex
dtype,则具有混合列数据类型:
In [48]: df1.join(df2)
...
skipped
...
ValueError: can only call with other PeriodIndex-ed objects
merge
抛出相同的异常:
In [54]: pd.merge(df1, df2, left_index=True, right_index=True)
...
skipped
...
ValueError: can only call with other PeriodIndex-ed objects
因此我们必须将 df1.columns
转换为字符串:
In [49]: df1.columns = df1.columns.values.astype(str)
In [50]: df1.columns
Out[50]: Index(['1996Q2', '2000Q3', '2010Q4'], dtype='object')
现在 join
和 merge
将起作用:
In [51]: df1.join(df2)
Out[51]:
1996Q2 2000Q3 2010Q4 a b c
0 1.5 3.5 1.000000 a1 b1 c1
1 22.0 38.5 2.000000 a2 b2 c2
2 15.0 35.0 4.333333 a3 b3 c3
In [52]: pd.merge(df1, df2, left_index=True, right_index=True)
Out[52]:
1996Q2 2000Q3 2010Q4 a b c
0 1.5 3.5 1.000000 a1 b1 c1
1 22.0 38.5 2.000000 a2 b2 c2
2 15.0 35.0 4.333333 a3 b3 c3
合并 DF 的列 dtypes
:
In [58]: df1.join(df2).columns
Out[58]: Index(['1996Q2', '2000Q3', '2010Q4', 'a', 'b', 'c'], dtype='object')
如果在合并完成后需要 df1.columns
作为 PeriodIndex
- 您可以在转换和设置之前保存 df1.columns
完成合并/加入后他们回来:
In [60]: df1.columns
Out[60]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')
In [61]: cols_saved = df1.columns
In [62]: df1.columns = df1.columns.values.astype(str)
In [63]: df1.columns
Out[63]: Index(['1996Q2', '2000Q3', '2010Q4'], dtype='object')
# merging (joining) or doing smth else here ...
In [64]: df1.columns = cols_saved
In [65]: df1.columns
Out[65]: PeriodIndex(['1996Q2', '2000Q3', '2010Q4'], dtype='period[Q-DEC]', freq='Q-DEC')
关于python - 值错误 : can only call with other PeriodIndex-ed objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40499756/
我可以像这样创建季度和月度 PeriodIndex: idx = pd.PeriodIndex(year=[2000, 2001], quarter=[1,2], freq="Q") # quarte
我的数据框的列为季度(PeriodIndex) - 例如。 2010 年第一季度、2010 年第二季度。此外,还有多个同名列,因为我已将每月数据转换为季度数据。我想要从 2011 年第 1 季度开始的
我正在尝试使用重新采样周期的初始值,以便与 first() 和 last() 一起使用。例如,我从每日数据开始,然后重新采样为每月数据: df.resample('M', kind='period')
我有一个 DataFrame,其中列是按月的 PeriodIndex,如下所示: df = pd.DataFrame(np.random.randn(3,4), index = np.arange(3
Pandas Series.to_json()函数在使用 PeriodIndex 时创建不可读的 JSON。 发生的错误是:json.decoder.JSONDecodeError: Expectin
我正在尝试获取多索引数据框中的最后一个可行日期: ix = pd.MultiIndex.from_product([["a"], pd.PeriodIndex(start="2017-01", end
我希望使用 periodIndex 系列并创建一个新系列,以“yyyy/yy”格式显示当前财政年度。例如,以英国财政年度为例 -> 01/04 至 31/03。 df = pd.DataFrame
pandas.PeriodIndex 上的文档举如下构造示例: >>> idx = PeriodIndex(year=year_arr, quarter=q_arr) 并指定这两个参数的类型: yea
我有一个关于将 tseries.period.PeriodIndex 转换为日期时间的问题。 我有一个如下所示的 DataFrame: colors count
我在绘制具有 PeriodIndex 的 pandas 数据框时遇到问题。 我的数据存在差距,我希望实现以下目标: 间隙应保持间隙,不进行插值; 由于每个值在整个时间段内都有效,因此该值应在整个时间段
在 DataFrame 上使用 panda 的 resample 函数以将刻度数据转换为 OHLCV 时,遇到重采样错误。 我们应该如何解决这个错误? data = pd.read_csv('tick
我正在处理一些组织为带有 MultiIndex 的 df 的财务数据。包含股票代码和日期以及包含返回的列。我想知道是否应该将索引转换为 PeriodIndex而不是 DateTimeIndex因为返回
我正在尝试将 2 个数据帧合并在一起。具有讽刺意味的是,它们最初是同一个数据框的一部分,但我正在迈出一小步——有时是在错误的方向上。第 1 帧看起来像这样: Int64Index: 10730 ent
我在 Python 中有一个 pandas 数据框,其中有一列如下: df.Timestamp ... .................. 129 2018-09-12 21:40:00
我正在尝试对常用的航空公司乘客数据集运行基本的season_decompose,该数据集以以下行开头: Month 1949-02 4.770685 1949-03 4.882802 19
我有一些要分析的订单数据。 目前感兴趣的是:哪个SKU在哪个月被购买的频率? 这里有一个小例子: import datetime import pandas as pd import numpy as
我在 pandas 中有这个数据框 key date story_point Story point 0 SOF-15
file_location3 = "F:/python/course1_downloads/City_Zhvi_AllHomes.csv" housing = pd.read_csv(file_loc
请帮帮我。我想基于 1D 重新采样。我有以下格式的数据。我想在 Pandas 中使用重采样。 我想根据日期和产品重新采样并填充缺失值。 但我一直犯这个错误:我尝试了 5 个选项,错误只在“instan
我是一名优秀的程序员,十分优秀!