Python Pandas Put a Value in a Specific Cell

Asked 2 years ago, Updated 2 years ago, 39 views

Hello.

I'm asking you a question because there's something I don't know while using Pandas.

In certain columns, I want only the first row of data to be entered and the rest to be empty, but I don't know what to do.

I looked it up and I thought it was possible, so I wrote it like "pd_data.loc" AttributeError: error 'dict' object has no attribute 'loc'. I don't know what to use to make it possible.

The Pandas processing part is as follows. I put '' in the Velu to make a column only.

pd_data = ({'ID':usr_id, 'Name':usr_name, 'Sex':usr_sex,
            'Address':usr_adr, 'Phone':PH_num, 'Column1':'', 'Column2':'',
            'Column3':'', 'Column4':''})
external_pd = pd.DataFrame(pd_data)

pd_data.loc[1][6] = value_a
pd_data.loc[1][7] = value_b
pd_data.loc[1][8] = value_c
pd_data.loc[1][9] = value_d

external.to_csv('result.csv', encoding='utf-8-sig')

Column1 column contains the value of the variable Value_a and Column2 column contains the value of the variable Value_b.

I want the previous columns to contain data one by one, and columns 1 through 4 only contain data in the first row, and the rest to be empty.

Masters, please share your knowledge with me. I beg you.

I have attached an image for your understanding. Finally, the table you want is as follows.

Image:https://res.cloudinary.com/eightcruz/image/upload/v1599219793/bm8xml5fmkump4w8lqak.png

Please understand that the image does not appear on the post, so you can copy and insert the link.

python pandas

2022-09-20 20:13

1 Answers

Read the error message carefully.

They say that pd_data is not a data frame but a dict.

Looking at the code, pd_data is still dict. The data frame is external_pd.


2022-09-20 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.