I want to multiply some of the elements in a column by 2 in a data frame

Asked 1 years ago, Updated 1 years ago, 357 views

The following data frames are available:
I'd like to double the value in the second column. There is no index and item name, so
What kind of code should I write?
I would appreciate it if you could let me know!
Enter a description of the image here

python pandas

2023-02-09 06:51

2 Answers

If you do not specify a column name, consecutive column names starting with 0 are automatically generated, so you can rewrite the value of the second column by specifying 1 in the column name.

sample code

import pandas as pd
from io import StringIO
US>"s="" 
2022/01/01 0:30:00,250
2022/01/01 1:00:00,200
""" 
df = pd.read_csv (StringIO(s), header = None)
df[1] = df[1] * 2 # Rewrite the second column
print(df)

Run Results

01
0  2022/01/01 0:30:00  500
1  2022/01/01 1:00:00  400


2023-02-09 07:27

import pandas as pd
importio

csv_data='"
2022/1/1 0:30,250
2022/1/1 1:00,220
'''
df=pd.read_csv(io.StringIO(csv_data), header=None)

#
print(df)
df.iloc[:,1]*=2
print(df)

#                0    1
# 0  2022/1/1 0:30  250
# 1  2022/1/1 1:00  220
#
#                0    1
# 0  2022/1/1 0:30  500
# 1  2022/1/1 1:00  440


2023-02-09 08:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.