CSV文件列处理:如何按指定方式混合ID_espacio与Espacio列?
ID_espacio and Espacio Columns in Your CSV Got it, let's figure out how to combine the ID_espacio and Espacio columns from your CSV file. Since you referenced a "specified example format" but didn't share the exact pattern, I'll go with the common intuitive pairing (like 264244 - NUEVO_Infotechnology - Home_IT - IT_Header for your first sample row) and cover multiple methods so you can pick what fits your workflow best.
1. Excel/Google Sheets (No-Code Approach)
Perfect if you prefer a visual interface:
- Insert a new blank column next to
Espacio(name it something likeID_Espacio_Combinedfor clarity) - In the first data row of this new column, enter the formula (adjust column letters to match your sheet):
=B2 & " - " & C2
Note: B =ID_espacio, C =Espacioin this example - Drag the fill handle down to apply the formula to all rows. If you want to replace the formula with static values, copy the new column, then use "Paste Values" to overwrite it.
2. Python with Pandas (For Large Datasets)
Great for automated or bulk processing:
First, install pandas if you haven't already:
pip install pandas
Then run this script (replace file names as needed):
import pandas as pd # Load your CSV file df = pd.read_csv("your_input_file.csv") # Combine the two columns with your desired format # Modify the string to match your exact required pattern df["ID_Espacio_Combined"] = df["ID_espacio"].astype(str) + " - " + df["Espacio"] # Save the processed CSV (without the index column) df.to_csv("your_output_file.csv", index=False)
Want a different format? Just tweak the concatenation line. For example, if you want [264244] | NUEVO_Infotechnology..., use:
df["ID_Espacio_Combined"] = "[" + df["ID_espacio"].astype(str) + "] | " + df["Espacio"]
3. Command Line with awk (Quick Terminal Fix)
Ideal if you prefer working in a terminal:
Run this command (adjust input/output filenames and separator if your CSV uses spaces after commas):
# For CSV with no spaces after commas awk -F ',' 'BEGIN{OFS=","} NR==1{print $0,"ID_Espacio_Combined"; next} {print $0,$2" - "$3}' your_input.csv > your_output.csv # For CSV with spaces after commas (like your sample data) awk -F ', ' 'BEGIN{OFS=", "} NR==1{print $0,"ID_Espacio_Combined"; next} {print $0,$2" - "$3}' your_input.csv > your_output.csv
Breakdown:
-F ', ': Sets the input separator to match your CSV's formatBEGIN{OFS=", "}: Ensures the output uses the same separatorNR==1: Handles the header row by adding the new column name- The rest of the code appends the combined ID+Espacio value to each data row
内容的提问来源于stack exchange,提问作者user9371527

