Home > Net >  How to add two columns of a Stata/CSV file in python
How to add two columns of a Stata/CSV file in python

Time:11-03

I have a CSV file with two columns (permno_adj and publn_year) and I want to combine them but don't know how to do it.

The code I am using:

patents = pd.read_csv('E:/Work/file.csv')

patents = patents[['publn_nr', 'permno_adj', 'publn_year', 'IPC1']].dropna().drop_duplicates().reset_index(drop=True)
patents = patents[(patents['publn_year'] >= 1980) & (patents['publn_year'] < 2016)].reset_index(drop=True)

print(patents)

The output I am currently getting i:

         publn_nr permno_adj  publn_year  IPC1
0            1830    US4060B        2005  F16F
1           24429    US4060A        2004  B29C
2           24943     US1794        2006  C08J
3           26115  US133366B        1999  A61B
4           31737    US4060A        2004  C08F

The output I am looking for is something like "US4060B2005"

CodePudding user response:

You could concatenate like string

patents['new_column'] = patents['permno_adj'].astype(str)   patents['publn_year'].astype(str)

CodePudding user response:

follow assign function pandas assign

d = {'col1': ['c11', 'c12'], 'col2': ['c21', 'c22']}
df = pd.DataFrame(d)
df = df.assign(col3=df['col1']   df['col2'])
df
  col1 col2    col3
0  c11  c21  c11c21
1  c12  c22  c12c22
  • Related