相关疑难解决方法(0)

在Python pandas中向现有DataFrame添加新列

我有以下索引的DataFrame与命名列和行不连续数字:

          a         b         c         d
2  0.671399  0.101208 -0.181532  0.241273
3  0.446172 -0.243316  0.051767  1.577318
5  0.614758  0.075793 -0.451460 -0.012493
Run Code Online (Sandbox Code Playgroud)

我想'e'在现有数据框中添加一个新列,并且不希望更改数据框中的任何内容(即,新列始终与DataFrame具有相同的长度).

0   -0.335485
1   -1.166658
2   -0.385571
dtype: float64
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的版本join,append,merge,但我没有得到我想要的结果,只在最错误.如何e在上面的示例中添加列?

python dataframe pandas chained-assignment

887
推荐指数
23
解决办法
182万
查看次数

如何从Pandas中的两列形成元组列

我有一个Pandas DataFrame,我希望将'lat'和'long'列组合成一个元组.

<class 'pandas.core.frame.DataFrame'>
Int64Index: 205482 entries, 0 to 209018
Data columns:
Month           205482  non-null values
Reported by     205482  non-null values
Falls within    205482  non-null values
Easting         205482  non-null values
Northing        205482  non-null values
Location        205482  non-null values
Crime type      205482  non-null values
long            205482  non-null values
lat             205482  non-null values
dtypes: float64(4), object(5)
Run Code Online (Sandbox Code Playgroud)

我试图使用的代码是:

def merge_two_cols(series): 
    return (series['lat'], series['long'])

sample['lat_long'] = sample.apply(merge_two_cols, axis=1)
Run Code Online (Sandbox Code Playgroud)

但是,这返回了以下错误:

---------------------------------------------------------------------------
 AssertionError                            Traceback (most recent call last)
<ipython-input-261-e752e52a96e6> in <module>()
      2     return (series['lat'], series['long'])
      3 …
Run Code Online (Sandbox Code Playgroud)

python tuples dataframe pandas

98
推荐指数
5
解决办法
7万
查看次数

标签 统计

dataframe ×2

pandas ×2

python ×2

chained-assignment ×1

tuples ×1