I have a question about Python data coupling.

Asked 2 years ago, Updated 2 years ago, 325 views

I have a question about Python data coupling.

Could you tell the series how to make df1 left external with the date key?

series=pd.series(['1/1', '1/2', ...'1/10', name='date')
df1=pd.DataFrame({'date':['1/1','1/2',...'1/7',...'weather':['clear,'cloud',...'cloud']})

python pandas

2022-09-30 22:03

1 Answers

import pandas as pd

pd.set_option('display.unicode.east_asian_width', True)

series = pd.Series(
  ['1/1', '1/2', '1/3', '1/4', '1/5', '1/6', '1/7', '1/8', '1/9', '1/10'],
  name = 'Date')

df1 = pd.DataFrame({
  'Date': ['1/1', '1/2', '1/3', '1/4', '1/5', '1/6', '1/7',
  Weather: ['Clear', 'Clear', 'Cloudy', 'Clear', 'Clear', 'Clear', 'Clear', 'Clear', 'Clear',
})

dfx=pd.merge(series, df1, on='date', how='left')
# dfx = df1.set_index('date') .reindex(series).reset_index()
print(dfx)

#
   date weather
01/1 Clear
1/2. Clear
2 1/3 Cloudy
3 1/4 sunny
41/5 Cloudy
51/6 Cloudy
6 1/7 Clear
7 1/8 NaN
81/9 NaN
91/10 NaN


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.