gpt4 book ai didi

python - pandas 执行 "true"concat

转载 作者:行者123 更新时间:2023-11-30 23:26:10 25 4
gpt4 key购买 nike

在我看来,当我进行连接时,pandas 返回一个包含两个系列而不是一个系列的数据帧。这给我带来了一些麻烦..

df1 = pandas.DataFrame(np.random.randn(4, 1), columns=['A'])
df2 = pandas.DataFrame(np.random.randn(4, 1), columns=['A'])

df3 = pandas.concat( [df1, df2] )
print df3
#Trying to isolate the row with the lowest value
print df3.ix[df3['A'].argmin()]

给我这个输出

          A
0 -1.368203
1 0.340653
2 -0.431968
3 -0.354293
0 0.391797
1 -0.263332
2 -1.450046
3 0.162143
[8 rows x 1 columns]

A
2 -0.431968
2 -1.450046
[2 rows x 1 columns]

正如您所看到的,问题在于它不会创建新索引,因此我没有得到一行而是两行。

如何“正确”地做到这一点?

最佳答案

您在寻找ignore_index=True吗?

In [8]:

df3 = pandas.concat( [df1, df2] , ignore_index=True)
print df3
#Trying to isolate the row with the lowest value
print df3.ix[df3['A'].argmin()]


A
0 -0.218089
1 -0.638552
2 0.955099
3 0.508360
4 -0.000249
5 0.125377
6 0.969202
7 -1.112411

[8 rows x 1 columns]
A -1.112411
Name: 7, dtype: float64

关于python - pandas 执行 "true"concat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22664572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com