gpt4 book ai didi

python - 为什么我的数据框列发生了变化?

转载 作者:行者123 更新时间:2023-12-01 02:05:22 25 4
gpt4 key购买 nike

我的代码

import pandas as pd
import numpy as np

series = pd.read_csv('o1.csv', header=0)

s1 = series
s2 = series

s1['userID'] = series['userID'] + 5
s1['adID'] = series['adID'] + 3
s2['userID'] = s1['userID'] + 5
s2['adID'] = series['adID'] + 4

r1=series.append(s1)
r2=r1.append(s2)

print(r2)

我出了点问题,现在列完全相同。输出

    userID gender  adID  rating
0 11 m 107 50
1 11 m 108 100
2 11 m 109 0
3 12 f 107 50
4 12 f 108 100
5 13 m 109 62
6 13 m 114 28
7 13 m 108 36
8 12 f 109 74
9 12 f 114 100
10 14 m 108 62
11 14 m 109 28
12 15 f 116 50
13 15 f 117 100
0 11 m 107 50
1 11 m 108 100
2 11 m 109 0

我不想更改我的系列专栏。为什么会发生这样的事?如何改变这一点?我需要使用 iloc 吗?

最佳答案

IIUC需要copy如果需要新对象DataFrame:

s1 = series.copy()
s2 = series.copy()

示例:

print (df)
userID gender adID rating
0 11 m 107 50
1 11 m 108 100
2 11 m 109 0


s1 = df.copy()
s2 = df.copy()

s1['userID'] = df['userID'] + 5
s1['adID'] = df['adID'] + 3
s2['userID'] = s1['userID'] + 5
s2['adID'] = df['adID'] + 4

r1=df.append(s1)
r2=r1.append(s2)

print(r2)
userID gender adID rating
0 11 m 107 50
1 11 m 108 100
2 11 m 109 0
0 16 m 110 50
1 16 m 111 100
2 16 m 112 0
0 21 m 111 50
1 21 m 112 100
2 21 m 113 0

关于python - 为什么我的数据框列发生了变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49131972/

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