Create new column in csv pandas
You can create a new column in a CSV file using pandas in Python by following these steps:
- Import the pandas library:
import pandas as pd
- Read the CSV file into a pandas DataFrame:
df = pd.read_csv('your_file.csv')
- Create a new column by assigning a value to a new column name:
df['new_column_name'] = [value1, value2,...]
Here,
value1
,value2
, etc. are the values you want to assign to the new column.
For example, if you want to create a new column called new_column
with values 1
, 2
, 3
, etc.:
df['new_column'] = range(1, len(df) + 1)
-
If you want to create a new column based on an existing column, you can use the
apply
method:df['new_column'] = df['existing_column'].apply(lambda x: x * 2)
This will create a new column
new_column
with values that are twice the values in theexisting_column
. -
If you want to create a new column based on multiple columns, you can use the
apply
method with a lambda function:df['new_column'] = df.apply(lambda row: row['col1'] + row['col2'], axis=1)
This will create a new column
new_column
with values that are the sum of the values incol1
andcol2
for each row. -
Finally, you can write the updated DataFrame back to the CSV file:
df.to_csv('your_file.csv', index=False)
This will overwrite the original CSV file with the updated DataFrame.
Here is an example code snippet that creates a new column new_column
with values 1
, 2
, 3
, etc.:
import pandas as pd
# Read the CSV file
df = pd.read_csv('your_file.csv')
# Create a new column
df['new_column'] = range(1, len(df) + 1)
# Write the updated DataFrame back to the CSV file
df.to_csv('your_file.csv', index=False)
Note that the index=False
parameter in the to_csv
method tells pandas not to write the row index to the CSV file.