Creating a column in Dataframe [duplicate] - python

This question already has answers here:
Add column with constant value to pandas dataframe [duplicate]
(4 answers)
Closed 3 years ago.
Very, very new to python and have a question:
Can someone tell me how to create a new date column that is the date this data was collected. For example, if this is from a Jan 1.xlsx file, this column should be full of Jan 1.
I know how to create the column but how do I populate with Jan 1? Right now I only have to do this with one file but I am going to have to do this for all 31 files for January.
All help greatly appreciated...

After you instatiate dataframe (read file into pandas object). Just do:
df["dt"]="Jan 1"
It will populate whole column with this 1 value, for all rows

Related

Final value in dataframe showing as not inside dataframe when going through for loop [duplicate]

This question already has answers here:
Python Pandas -- why does the `in` operator work with indices and not with the data?
(1 answer)
How to determine whether a Pandas Column contains a particular value
(11 answers)
Closed 1 year ago.
I'm checking to see if all the values in one column of one dataframe lies in the column of another dataframe. When I run the code below, it says that 4 does not exist in df1. Is there any particular reason for this?
list1=[1,2,3,4]
list2=[1,2,3,4]
df2=pd.DataFrame(list2)
df2.rename(columns={0:"List2"},inplace=True)
df1=pd.DataFrame(list1)
df1.rename(columns={0:"List1"},inplace=True)
for i in df2['List2']:
if i not in df1['List1']:
print(i)

How to query from multiindex dataframe containing data and time as index [duplicate]

This question already has answers here:
selecting from multi-index pandas
(7 answers)
Closed 1 year ago.
Check this image of dataframe
I've posted the picture of dataframe I am working with, I want to pull out data from specific times of a certain date
I've tried
stockdf.loc[("2015-01-01")].loc['09:17:00']
stockdf.loc[("2015-01-01","09:17:00"),:]
Both do not work.
Just try:
stockdf.loc[("2015-01-01", "09:17:00")]
If they're dates:
stockdf.loc[(pd.to_datetime("2015-01-01").date(), pd.to_datetime("09:17:00").time())]

Filter columns of the dataframe with specific string [duplicate]

This question already has answers here:
how to read certain columns from Excel using Pandas - Python
(6 answers)
Selecting multiple columns in a Pandas dataframe
(22 answers)
Closed 2 years ago.
I need to filter data of the columns according to string values, for example, I only need the columns that contain:
['CEMENTO MELON', 'G_CTRL_TE_DIEGO_DE_ALMAGRO','G_CTRL_EO_CANELA_2']
todos = []
for f in glob.glob('*.xlsx'):
#Here must go the code
todos.append(df)
I would greatly appreciate your help.

how can I remove index(auto made) in pandas.read_csv [duplicate]

This question already has answers here:
Removing index column in pandas when reading a csv
(9 answers)
Closed 3 years ago.
here is my dataframe
my csv file is
date,open,high,low,close,volume,cap,Unnamed: 7
20190816,28600,28850,28150,28350,335508,6065213000000,
20190814,29550,29600,28800,28950,296026,6193563000000,
20190813,29400,29900,29400,29550,196955,6321927000000,
20190812,29450,30350,29400,29850,166580,6386109000000,
20190809,29500,30300,29450,29750,468338,6364715000000,
20190808,29000,30000,29000,29650,448959,6343321000000,
20190807,29800,29800,28950,29000,431524,6204260000000,
20190806,30900,30950,29650,29900,710348,6396806000000,
20190805,30300,31100,30300,30950,608970,6621443000000,
20190802,30400,30750,29900,30400,420984,6503776000000,
I don't know why 0 ~ 11 index exists
I want to remove this (0~11)
I searched and tried index_col=False, index_col=None and to_csv with index=False but The problem was not resolved.
how can I remove this index(0~11)?
Your valuable opinions and thoughts will be very much appreciated.
The only solution that fully matches what you desire is to create a string:
print(df.to_string(index=False))
Another solution would be the below, it will still be a dataframe, but just the first column's values will be shifted down one level:
print(df.set_index('date'))
You cannot remove the index of a Pandas DataFrame. It is not one of the columns of your DataFrame. And it is not coming from the csv file.
See: https://stackoverflow.com/a/20107825/4936825
You can not display this automatically generated index with „df.style.hide_index()“ or set one of the colums as index with set_index() method.

whole column as datetime.date(Y,m,d) in python pandas [duplicate]

This question already has answers here:
How to change the datetime format in Pandas
(8 answers)
Keep only date part when using pandas.to_datetime
(13 answers)
Closed 4 years ago.
When I read data from database (or csv, etc.) pandas keep storing date-only data as timestamp and adds 00:00:00? Is there a way to enforce that the column is of type datetime.date and therefore no time is stored?
I tried this but it seems the 00:00:00 is more sticky than originally thought.
pd.to_datetime(mydf['date'], format='%Y-%m-%d').iloc[0]
Out[68]: Timestamp('2015-03-27 00:00:00')
Can i have instead of Timestamp the whole column be of type just datetime.date?

Categories

Resources