How to write or overwrite a column of csv from pd.Series, where the later is longer than original row lines? I use the following code:
df.loc[:, col_name] = pd.Seriese(new_value)
Where col_name
is the column need to be modified, new_value
is a list containing new values, this list is longer than table's row lines.
Then write csv with df.to_csv(file_name, index=False, header=True)
But it only writes original maximal rows of table.
How about using pd.concat
?
Before:
df1 = pd.DataFrame(data=[
[2,3],
[2,3],
[1,5],
[3,3]
], columns=['Test1', 'Test2'])
>> Test1 Test2
0 2 3
1 2 3
2 1 5
3 3 3
Cast Series longer than number of rows into a DF:
df2 = pd.DataFrame(
data=[9,8,2,1,5],
columns=["Test1"]
)
Concat remaining column(s) with axis=1
to concatenate column-wise:
pd.concat([
df2,
df1[['Test2']]
], axis=1)
>> Test1 Test2
0 9 3.0
1 8 3.0
2 2 5.0
3 1 3.0
4 5 NaN
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.