Updating a pandas DataFrame row with a dictionary

前端 未结 1 1400
萌比男神i
萌比男神i 2021-02-09 01:59

I\'ve found a behavior in pandas DataFrames that I don\'t understand.

df = pd.DataFrame(np.random.randint(1, 10, (3, 3)), index=[\'one\', \'one\', \'two\'], colu         


        
相关标签:
1条回答
  • 2021-02-09 02:41

    It's the difference between how a dictionary iterates and how a pandas series is treated.

    A pandas series matches it's index to columns when being assigned to a row and matches to index if being assigned to a column. After that, it assigns the value that corresponds to that matched index or column.

    When an object is not a pandas object with a convenient index object to match off of, pandas will iterate through the object. A dictionary iterates through it's keys and that's why you see the dictionary keys in that rows slots. Dictionaries are not sorted and that's why you see shuffled keys in that row.

    0 讨论(0)
提交回复
热议问题